Crelix Crelix - 1 month ago 8
PHP Question

PHP insert into MySQL only that variables that have been given by other function

Good Morning. In my mind I have some interesting coding, but I don't know how to make it happen in real code. At this moment, with posted code both function works, but it inserts data in wrong fields.


Maybe someone has some tips, or examples how to do it.


What I want to do:
I want to record all user actions into MySQL database. Main Idea is that, I use one function for different insert types!? (Insert only values, that are given by that action).

Its because I got about 20 different actions (
comment
,
add post
,
follow
, and more). And they have got different variables.

My idea is to pass variables from actions to this insert function.

For example:

If user
add new post
action pass
user_id
,
action
(String with text),
post_id
, all other fields stay null.

If user
starts following
action pass
user_id
,
action
,
user_id
(that follows) , all other fields stay null.

If user
comments on post
action pass
user_id
,
action
,
comment_id
,
post_id
, all other fields stay null.

My database table
user_actions

Image

I know how to pass these values to function, but don't how to make
Insert Function
that receives these values and then insert only that values, who has been received.

After functions has been called, with below given data it inserts into wrong database field like this: (
addpost
field inserted in
follow
field)
Image

This is how I'm
sending
data

public function uploadPost($title,$image){
try{

if(isset($_SESSION['user_session'])){
$user_id = $_SESSION['user_session'];

$stmt = $this->runQuery("SELECT * FROM users WHERE id=:id");
$stmt->execute(array(":id"=>$user_id));

$myRow=$stmt->fetch(PDO::FETCH_ASSOC);

}else{
$_SESSION["error"]='<div class="alert alert-danger alert-dismissible fade in" role="alert"><button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>Sorry, You have to login in your profile!</div>';
echo $_SESSION["error"];
exit();
}

$addedby = $myRow['id'];

$stmt = $this->conn->prepare("INSERT INTO fun_posts(title,image,addedby) VALUES(:title, :image, :addedby)");

$stmt->bindparam(":title", $title);
$stmt->bindparam(":image", $image);
$stmt->bindparam(":addedby", $addedby);

$stmt->execute();



include("../System/class.actions.php");
$actions = new ACTIONS();
$action = "Test";
$addedby = $addedby;
$addpost = "1";
$actions->insertAction($action,$addedby,$addpost);

$_SESSION["result"]='<div class="alert alert-success alert-dismissible fade in" role="alert"><button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>You have succesfully uploaded new post!</div>';
echo $_SESSION["result"];
exit();

}
catch(PDOException $e)
{
echo $e->getMessage();
}
}


This is how I
receive
data:

public function insertAction($action, $addedby, $follow, $unfollow, $comment, $reply, $likecomment,
$likepost, $addimage, $addpost){
try{

$stmt = $this->conn->prepare("INSERT INTO user_actions(action,addedby,follow,unfollow,comment,reply,
likecomment,likepost,addimage,addpost) VALUES(:action, :addedby, :follow, :unfollow, :comment, :reply,
:likecomment, :likepost, :addimage, :addpost)");

$stmt->bindparam(":action", $action);
$stmt->bindparam(":addedby", $addedby);
$stmt->bindparam(":follow", $follow);
$stmt->bindparam(":unfollow", $unfollow);
$stmt->bindparam(":comment", $comment);
$stmt->bindparam(":reply", $reply);
$stmt->bindparam(":likecomment", $likecomment);
$stmt->bindparam(":likepost", $likepost);
$stmt->bindparam(":addimage", $addimage);
$stmt->bindparam(":addpost", $addpost);

$stmt->execute();

$_SESSION["result"]='<div class="alert alert-success alert-dismissible fade in" role="alert"><button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>Action has been succesfully inserted!</div>';
echo $_SESSION["result"];
exit();

}
catch(PDOException $e)
{
echo $e->getMessage();
}
}

Answer Source

This is the problem:

$actions->insertAction($action,$addedby,$addpost);

Parameters are just sent to the function in order, so it thinks you gave it the parameters $action, $addedby, $follow of the insertAction method.

Just because you named the variable "$addpost" in your calling function is meaningless, PHP doesn't use that variable name when you call the function, it just uses the position that you placed the variable in, in comparison to the input parameters. Variable names in one scope have no equivalence to variable names in another scope, that's not how it works.

So actually the code is not doing anything wrong, it's doing exactly what you told it to.

You need to write null for all the parameters you aren't going to pass to the function. Or, have insertAction accept a single associative array of parameters, then you can name them within the array that you pass in, and only pass the ones you need each time.