Insert returning null and no error message from the script


#1

i am trying to insert data to my db and it’s returning null when i var_dump ($insert). i need help here. I need a third eye i can’t figure out what the problem might be. thanks

if (isset($_POST['submit']))

	{			//list out your form variables...
			$property_name=$_POST['property_name'];
			$property_location=$_POST['property_location'];
			//$property=$_FILES['property']['name'];
			$property_price=$_POST['property_price'];
			$category_id=$_POST['category_id'];
			$status=$_POST['status'];
//validation

           $sql="INSERT INTO `property` (property_name, property_location, property_price, category_id, status)VALUES (?, ?, ?, ?, ?)";

          	     $params=[$property_name, $property_location, $property_price, 
							$category_id, $status];

    	   $insert=$db->runQuery($sql,$params);
							if($insert){
								
								echo "good";
							}
							else { echo "bad";}
						
          	     var_dump($sql);
          	     var_dump($params);
          	    var_dump($insert);
         	     

          	     die();
}

here is my function

public function runQuery($query,$params=[]){
	try{		
	$stmt=$this->datab->prepare($query);
	$stmt->execute($params);
	
	}catch (PDOException $e){
	throw new Exception($e->getMessage());
	}
	} 

#2

What is the function code behind $db->runQuery ?


#3

As runQuery() doesn’t return anything, surely it’s no surprise that $insert is null?

Does it insert the data into the table?


#4

No. it does’t


#5

If you run the query with the same values from phpmyadmin, does it insert the data?


#6

Are you setting the ERRMODE on your database connection object?
If not, PDO wont emit an Exception for a failed query. (The default is ERRMODE_SILENT)


#7

yes it insert data when i use phpmyadmin.


#8

yes. $this->datab->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->datab->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);


#9

What is the value of $stmt inside your function after you’ve called prepare(), and after you’ve called execute() ?


#10

i get this
PDOStatement Object ( [queryString] => INSERT INTO property (property_name, property_location, property_price, category_id, status)VALUES (?, ?, ?, ?, ?) )


#11

What does execute() return?

$x = $stmt->execute($params);
var_dump($x);

#12

it output boolean true;


#13

Doesn’t that mean that it is working correctly? So all you need to do is either store that object in your class somewhere, or return it to the calling code, then retrieve data from it?


#14

It would appear so in this case, but i always caution using this phrasing;
Returning true doesnt mean it worked correctly, it means it didn’t encounter an error.
Those can be very different things, sometimes :wink:


#15

i must be honest i don’t understand which object you are talking about. i have one class which is database. and i created an instance of that class. is that it.


#16

then what do you suggest. i do?


#17

i use this same function to retrieve data and it’s working perfect.


#18

I suggest you do what has been suggested to you and actually return a value from the function.

How are you checking that the insert isnt happening?


#19

var_dump($insert) returns null.


#20

Which actually has nothing to do with your query. So the answer is “I’m not.” Also probably means you’ve got a bunch of duplicate rows in your database at this point, but that’s cleanup for afterwards.

return the value of the execute command. Or, alternatively, dont even check if ($insert) - you’ve set your database to throw an exception if something went wrong, and i assume you’ve set your exception handler to die() the script, so if the code even gets to the point of returning from the function, you know it worked.