aron pascua aron pascua - 2 years ago 79
MySQL Question

How to UPDATE in database using mysqli prepared?

So I'm using mysqli before to my query and now I convert it to mysqli prepared. I'm trying to update a particular data with upload image and I don't know why I get the error

mysqli_stmt_bind_result(): Number of bind variables doesn't match number of fields in prepared statement in line 30
and also how can I execute the query in mysqli query like
mysqli_query($conn, $query)

Below is the code of my UPDATE query:

if (isset($_POST['submit'])) {

$imageName = mysqli_real_escape_string($conn, $_FILES["latest_photo"]["name"]);
$imageData = mysqli_real_escape_string($conn, file_get_contents($_FILES["latest_photo"]["tmp_name"]));
$imageType = mysqli_real_escape_string($conn, $_FILES["latest_photo"]["type"]);

if (substr($imageType, 0,5) == "image") {

$query = "UPDATE `crew_info` SET `updated_photo` = ? WHERE `id` = ?";
$stmt = mysqli_prepare($conn, $query);
mysqli_stmt_bind_param($stmt, 'ss', $imageData, $_GET['id']);
mysqli_stmt_bind_result($stmt, $id, $updated_photo);

echo "Image Uploaded";


else {

echo "Image is not uploaded!";



In the code above, there is a comment line on how to execute the query. How can I do that? thank you guys


Also when I click the button Upload, it says that the image is uploaded but does not appear in database. why is that?

Answer Source
 //for procedural way
  $query = "UPDATE `crew_info` SET `updated_photo` = ? WHERE `id` = ?";
  $stmt = mysqli_prepare($conn, $query);
  // you should use i instead of s for id
  mysqli_stmt_bind_param($stmt, 'si', $imageData, $_GET['id']);

//try this out in object oriented
   $query = "UPDATE `crew_info` SET `updated_photo` = ? WHERE `id` = ?";
   $stmt = mysqli_prepare($conn, $query);
   $stmt->bind_param("si", $imageData, $id);
   $imageData=$imageName ;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download