Simber33 Posted March 27, 2013 Share Posted March 27, 2013 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! Quote Link to comment https://forums.phpfreaks.com/topic/276224-mysqli-error-call-to-a-member-function-execute-on-a-non-object/ Share on other sites More sharing options...
mac_gyver Posted March 27, 2013 Share Posted March 27, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/276224-mysqli-error-call-to-a-member-function-execute-on-a-non-object/#findComment-1421427 Share on other sites More sharing options...
Simber33 Posted March 27, 2013 Author Share Posted March 27, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/276224-mysqli-error-call-to-a-member-function-execute-on-a-non-object/#findComment-1421431 Share on other sites More sharing options...
mac_gyver Posted March 27, 2013 Share Posted March 27, 2013 see the use of the ? place holders and bind_parm() in an insert query at this link http://www.php.net/manual/en/mysqli-stmt.execute.php Quote Link to comment https://forums.phpfreaks.com/topic/276224-mysqli-error-call-to-a-member-function-execute-on-a-non-object/#findComment-1421435 Share on other sites More sharing options...
Solution Simber33 Posted March 27, 2013 Author Solution Share Posted March 27, 2013 Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/276224-mysqli-error-call-to-a-member-function-execute-on-a-non-object/#findComment-1421439 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.