majorwitty Posted September 19, 2012 Share Posted September 19, 2012 Hi, I receive errors while running post_message.php. Warning: mysqli_stmt_bind_param() expects parameter 1 to be mysqli_stmt, boolean given in /ABSOLUTEPATH/post_message.php on line 24 Below is my code, only difference between book and mine is connecting to database portion. I removed that and put appropriate data and still same error. Anyone able to help? I think $stmt has wrong value but I'm not sure how to confirm the appropriate value. if ($_SERVER['REQUEST_METHOD'] == 'POST') { // Connect to database: require ('../../../../phpfiles/mysqli_connect.php'); // Make the query: $q = 'INSERT INTO messages (forum_id, parent_id, user_id, subject, body, date_entered) VALUES (?, ?, ?, ?, ?, NOW())'; // Prepare the statement: $stmt = mysqli_prepare($dbc, $q); // Bind the variables: mysqli_stmt_bind_param($stmt, 'iiiss', $forum_id, $parent_id, $user_id, $subject, $body); // Assign the values to variables: $forum_id = (int) $_POST['forum_id']; $parent_id = (int) $_POST['parent_id']; $user_id = 3; // The user_id value would normally come from the session. $subject = strip_tags($_POST['subject']); $body = strip_tags($_POST['body']); // Execute the query: mysqli_stmt_execute($stmt); // Print a message based upon the result: if (mysqli_stmt_affected_rows($stmt) == 1) { echo '<p>Your message has been posted.</p>'; } else { echo '<p style="font-weight: bold; color: #C00">Your message could not be posted.</p>'; echo '<p>' . mysqli_stmt_error($stmt) . '</p>'; } // Close the statement: mysqli_stmt_close($stmt); // Close the connection: mysqli_close($dbc); } // End of forum submission phpinfo() shows: PHP Version 5.3.13 mysqli Mysqli Support enabled Client API library version 5.1.63 Active Persistent Links 0 Inactive Persistent Links 0 Active Links 0 Client API header version 5.1.61 MYSQLI_SOCKET /No-MySQL-hostname-was-specified Link to comment Share on other sites More sharing options...
margaux Posted September 19, 2012 Share Posted September 19, 2012 Your query is trying to insert 6 values but the mysql_stmt_bind_param is only 'binding' 5 parameters to the statement. You need to change it to $q = 'INSERT INTO messages (forum_id, parent_id, user_id, subject, body, date_entered) VALUES (?, ?, ?, ?, ?, ?)'; // Prepare the statement: $stmt = mysqli_prepare($dbc, $q); // Bind the variables: mysqli_stmt_bind_param($stmt, 'iiisss', $forum_id, $parent_id, $user_id, $subject, $body, NOW()); 1 Link to comment Share on other sites More sharing options...
Edward Posted September 20, 2012 Share Posted September 20, 2012 I usually set my variables first before preparing statement for binding, you only need to bind the 5 variables, just assign date_entered the SQL NOW() function, no need to try and bind SQL functions, stick to variables only. // Make the query: $q = 'INSERT INTO messages (forum_id, parent_id, user_id, subject, body, date_entered=NOW()) VALUES (?, ?, ?, ?, ?)'; // Prepare the statement: $stmt = mysqli_prepare($dbc, $q); // Assign the values to variables: $forum_id = (int) $_POST['forum_id']; $parent_id = (int) $_POST['parent_id']; $user_id = 3; // The user_id value would normally come from the session. $subject = strip_tags($_POST['subject']); $body = strip_tags($_POST['body']); // Bind the variables: mysqli_stmt_bind_param($stmt, 'iiiss', $forum_id, $parent_id, $user_id, $subject, $body); // Execute the query: mysqli_stmt_execute($stmt); Link to comment Share on other sites More sharing options...
HartleySan Posted September 20, 2012 Share Posted September 20, 2012 Edward, I think you're missing a right parenthesis after NOW(). Link to comment Share on other sites More sharing options...
Edward Posted September 20, 2012 Share Posted September 20, 2012 Yeah i must of deleted it out when i was adding in the NOW() well if that was my code and running it i would of found that easy when running, i don't have the full app here. Edited thanks, now its perfect. Link to comment Share on other sites More sharing options...
HartleySan Posted September 20, 2012 Share Posted September 20, 2012 Yeah, I figured it was an innocent mistake. I just didn't want majorwitty to get caught up on that small error when your advice was sound. Link to comment Share on other sites More sharing options...
margaux Posted September 20, 2012 Share Posted September 20, 2012 I usually set my variables first before preparing statement for binding, you only need to bind the 5 variables, just assign date_entered the SQL NOW() function, no need to try and bind SQL functions, stick to variables only. That's a better way to do it. Thanks Edward, I learn alot from your posts. Link to comment Share on other sites More sharing options...
majorwitty Posted September 20, 2012 Author Share Posted September 20, 2012 Thanks for suggestions. I still encounter an issue. Here is updated code: if ($_SERVER['REQUEST_METHOD'] == 'POST') { // Validate the data (omitted)! // Connect to database: require ('../../../../phpfiles/mysqli_connect.php'); // Check db connection if (!$dbc) { die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error()); } echo 'Success... ' . mysqli_get_host_info($dbc) . "\n"; // Make the query: $q = 'INSERT INTO messages (forum_id, parent_id, user_id, subject, body, date_entered=NOW()) VALUES (?, ?, ?, ?, ?)'; // Prepare the statement: $stmt = mysqli_prepare($dbc, $q); // Assign the values to variables: $forum_id = (int) $_POST['forum_id']; $parent_id = (int) $_POST['parent_id']; $user_id = 3; // The user_id value would normally come from the session. $subject = strip_tags($_POST['subject']); $body = strip_tags($_POST['body']); // Bind the variables: mysqli_stmt_bind_param($stmt, 'iiiss', $forum_id, $parent_id, $user_id, $subject, $body); // Execute the query: mysqli_stmt_execute($stmt); // Print a message based upon the result: if (mysqli_stmt_affected_rows($stmt) == 1) { echo '<p>Your message has been posted.</p>'; } else { echo '<p style="font-weight: bold; color: #C00">Your message could not be posted.</p>'; echo '<p>' . mysqli_stmt_error($stmt) . '</p>'; } // Close the statement: mysqli_stmt_close($stmt); Here is the full output from web browser accessing post_message.php: Success... practicedb.domainname.info via TCP/IP Warning: mysqli_stmt_bind_param() expects parameter 1 to be mysqli_stmt, boolean given in /[ABSOLUTEPATH]/post_message.php on line 39 Warning: mysqli_stmt_execute() expects parameter 1 to be mysqli_stmt, boolean given in /[ABSOLUTEPATH]/post_message.php on line 42 Warning: mysqli_stmt_affected_rows() expects parameter 1 to be mysqli_stmt, boolean given in /[ABSOLUTEPATH]/post_message.php on line 45 Your message could not be posted. Warning: mysqli_stmt_error() expects parameter 1 to be mysqli_stmt, boolean given in /[ABSOLUTEPATH]/post_message.php on line 49 Warning: mysqli_stmt_close() expects parameter 1 to be mysqli_stmt, boolean given in /[ABSOLUTEPATH]/post_message.php on line 53 Link to comment Share on other sites More sharing options...
Edward Posted September 20, 2012 Share Posted September 20, 2012 Can you show us the messages table SQL so we can check the data types? I like to do these prepared statements the object way, and what i have is almost identical to your statement but worked for me, so not sure what is up with yours: $q = 'INSERT INTO address (user_id, address, city, state_code, province, zip, country_id) VALUES (?, ?, ?, ?, ?, ?, ?)'; // Prepare the statement: $stmt = $mysqli->prepare($q); // Bind the variables: $stmt->bind_param('isssssi', $user_id, $address, $city, $state, $province, $zip, $country); // Execute the query: $stmt->execute(); That's a better way to do it. Thanks Edward, I learn alot from your posts. Thank you buddy, i learn from you also! 1 Link to comment Share on other sites More sharing options...
majorwitty Posted September 20, 2012 Author Share Posted September 20, 2012 Your suggestions have helped me out, thank you! When I looked at messages table I noticed the db name was different. My mysqli_connect.php file had incorrect DBNAME value. Up to Chapter 13 in book I've created three DBs. My mysqli_connect was pointing to user registration versus forum DB. Once I fixed mysqli_connect.php (I now use mysqli_connect_[dbname].php files) I continued to get same errors. I then changed the code to be identical to the book: // Make the query: $q = 'INSERT INTO messages (forum_id, parent_id, user_id, subject, body, date_entered) VALUES (?, ?, ?, ?, ?, NOW())'; // Prepare the statement: $stmt = mysqli_prepare($dbc, $q); // Assign the values to variables: $forum_id = (int) $_POST['forum_id']; $parent_id = (int) $_POST['parent_id']; $user_id = 3; // The user_id value would normally come from the session. $subject = strip_tags($_POST['subject']); $body = strip_tags($_POST['body']); // Bind the variables: mysqli_stmt_bind_param($stmt, 'iiiss', $forum_id, $parent_id, $user_id, $subject, $body); Again, thanks for the assistance! Link to comment Share on other sites More sharing options...
Recommended Posts