Tomy02 Posted April 1, 2013 Share Posted April 1, 2013 Im new to prepared statement and after a lot of research and reading, i still cant get it right. I have created a small database with user_id auto, name, lastname, and tried to make an insert with prepared statement. I have tried it several ways but still cant spot the error. Here is my 1 code. <?php $mysqli = new mysqli ('localhost', 'root', '','lr') or die ('there is a problem'); if (!($stmt = $mysqli->prepare("INSERT INTO pps(name) VALUES (?)"))) { echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error; } /* Prepared statement, stage 2: bind and execute */ $username = "john"; if (!$stmt->bind_param($username)) { echo "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error; } if (!$stmt->execute()) { echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error; } $stmt->close(); ?> This above code, gives me Warning: Wrong parameter count for mysqli_stmt::bind_param() in line 9. Binding parameters failed: (0) Execute failed: (2031) No data supplied for parameters in prepared statement. Note: Line 9 is this : $username = "john"; Then i tried this code which also fails. <?php $mysqli = new mysqli ('localhost', 'root', '','lr') or die ('there is a problem'); $query = "INSERT INTO pps (name, lastname) VALUES (?,?,)"; $stmt = $mysqli->prepare($query); $val1 = 'John'; $val2 = 'Lastname'; $stmt->bind_param("ss", $val1, $val2); /* Execute the statement */ $stmt->execute(); $val1 = 'Mark'; $val2 = 'Lastnamel'; /* Execute the statement */ $stmt->execute(); /* close statement */ $stmt->close(); $mysqli->close(); ?> This code results in Fatal error: Call to a member function bind_param() on a non-object in C:\xampp\htdocs\PreparedStatement\test.php on line 9. How can i correct this ? Quote Link to comment https://forums.phpfreaks.com/topic/276392-need-help-with-prepared-statement/ Share on other sites More sharing options...
Solution mikosiko Posted April 1, 2013 Solution Share Posted April 1, 2013 (edited) in the first snippet your bind_param() sentence is incomplete... read: http://php.net/manual/en/mysqli-stmt.bind-param.php in the second one you have a wrong sql sentence....you have an extra , here VALUES (?,?,) Edited April 1, 2013 by mikosiko Quote Link to comment https://forums.phpfreaks.com/topic/276392-need-help-with-prepared-statement/#findComment-1422313 Share on other sites More sharing options...
Tomy02 Posted April 1, 2013 Author Share Posted April 1, 2013 Got it! What a great community we have here, kudos mikosiko. Quote Link to comment https://forums.phpfreaks.com/topic/276392-need-help-with-prepared-statement/#findComment-1422324 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.