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 ?