Insert returning null and no error message from the script

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());
	}
	} 

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

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

Does it insert the data into the table?

No. it does’t

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

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)

yes it insert data when i use phpmyadmin.

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

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

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

What does execute() return?

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

it output boolean true;

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?

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:

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.

then what do you suggest. i do?

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

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?

var_dump($insert) returns null.

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.