I have the following code:
<?php
$dbhost = 'localhost';
$dbuser = 'user';
$dbpass = 'password';
$db = new mysqli($dbhost, $dbuser, $dbpass, 'images_db');
if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}
else{
echo "Connected to database";
}
//filename, mime_type and file_size are columns in the table images
$stmt = $db->prepare("INSERT INTO images (filename, mime_type, file_size) VALUES (?, ?, ?)");
$string1 = 'string 1';
$string2 = 'string 2';
$stmt->bind_param('ssi', $string1, $string2, 123);
$stmt->execute();
$stmt->close();
$mysqli->close();
?>
When I execute the code, nothing gets added to the mysql database. But when I comment out the line
$stmt->bind_param('ssi', $string1, $string2, 123);
and insert the string and integer values directly into the $db->prepare statement (replacing the question marks), it all works nicely and the row is added to the database table.
What am I doing wrong in the bind_param line that is preventing the new row being added to the database?
mysqli_stmt_bind_param accepts variables (by reference). You cannot use literals. Change your code to
$fileSize = 123;
$stmt->bind_param('ssi', $string1, $string2, $fileSize);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With