Jump to content

MySqli error - Call to a member function execute() on a non-object


Simber33

Recommended Posts

Hello, I´m trying to insert data from a form into a mysql table using mysqli and php. I use the code below to connect to the database:

 

$host = "myhost";
$db = "a5066994_tutors";
$user = "a5066994_tutors";
$pass = "mypassword";

$connection = mysqli_connect("$host", "$user", "$pass", "$db");

if ($connection->errno) {
printf("Connect failed: %s\n", $connection->error);
exit();

 

and the code below to insert:

$stmt = $connection->prepare("INSERT INTO tutorials (Author, Website, Title, Body1, Body2,
Body3, Body4, Subtitle1, Subtitle2, Subtitle3, Subtitle4, Category,
WTitle, Userid) VALUES ('$author','$website', '$title', '$text1', '$text2', '$text3', '$text4',
'$s1', '$s2', '$s3', '$s4', '$cat', '$wtitle', '$userid')
");

 

And it results in this error:

Call to a member function execute() on a non-object

 

I have also tried doing a var dump of the connection, which results in: object(mysqli)#1 (0) { }

and a var dump of the statement, which results in: bool(false)

 

Any help would be great, thank you!

Link to comment
Share on other sites

what does putting the following debugging code after the prepare() statement show -

printf("Prepare failed: %s\n", $connection->error); 


i'm going to guess that the values you are inserting in the query are from a form?. why are you using a prepared statement, but still directly putting the data into the query. that bypasses what a prepared query is for. a prepared query should use placeholders for the data values and then supply the data when the query is executed.

Link to comment
Share on other sites

Thanks! I found what the problem was - there is an unknown column. And yes, I'm new to using mysqli, so Im not sure how to use the prepared statement. Could you tell me how to use it? Thank you for your time!

 

what does putting the following debugging code after the prepare() statement show -

printf("Prepare failed: %s\n", $connection->error); 


i'm going to guess that the values you are inserting in the query are from a form?. why are you using a prepared statement, but still directly putting the data into the query. that bypasses what a prepared query is for. a prepared query should use placeholders for the data values and then supply the data when the query is executed.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.