PHP Fatal error: Uncaught Error: Call to a member function execute()


#1

Hello :slightly_smiling_face:,
I started with php recently and now I wanted to try my luck databases, but my website loads indefinitely when I send the form and run the php script.
The error is, according to error log, in this line, but I can’t find it.

Maybe you can help me.

	$servername = "localhost";
	$username = "username";
	$password = "password";
	$dbname = "dbname";
	$conn = new mysqli($servername, $username, $password, $dbname);

	$sql = "INSERT INTO tbl_car (title,description,address,city,state,zip_code,country,map,car_category_id,brand_id,model_id,body_type_id,fuel_type_id,transmission_type_id,vin,car_condition,engine,engine_size,exterior_color,interior_color,seat,door,top_speed,kilometer,mileage,year,warranty,featured_photo,regular_price,sale_price,seller_id,status) VALUES (".$_POST['title'].",".$_POST['description'].",".$_POST['address'].",".$_POST['city'].",".$_POST['state'].",".$_POST['zip_code'].",".$_POST['country'].",".$_POST['map'].",".$_POST['car_category_id'].",".$_POST['brand_id'].",".$_POST['model_id'].",".$_POST['body_type_id'].",".$_POST['fuel_type_id'].",".$_POST['transmission_type_id'].",".$_POST['vin'].",".$_POST['car_condition'].",".$_POST['engine'].",".$_POST['engine_size'].",".$_POST['exterior_color'].",".$_POST['interior_color'].",".$_POST['seat'].",".$_POST['door'].",".$_POST['top_speed'].",".$_POST['kilometer'].",".$_POST['mileage'].",".$_POST['year'].",".$_POST['warranty'].",".$final_name.",".$_POST['regular_price'].",".$_POST['sale_price'].",".$_SESSION['seller']['seller_id'].",0)";
	$sql->execute(); <--- This is line 156

Error Log:

[25-Feb-2019 15:19:47 Asia/Dhaka] PHP Fatal error: Uncaught Error: Call to a member function execute() on string in C:…\car-add.php:156
Stack trace:
#0 {main}
thrown in C:…\car-add.php on line 156


#2

All of that is bad, but if you look at your error logs, you will see that there is a problem with the query.


#3
$stmt = $conn->prepare($sql);


$stmtb->execute();

First prepare queries and then execute them. Hope it helps @MrNancy