如何使用mysqli准备在数据库中更新?

时间:2016-06-01 03:07:49

标签: php mysql mysqli

所以我之前使用mysqli查询,现在将其转换为mysqli编写。我正在尝试使用上传图片更新特定数据,我不知道为什么会收到错误mysqli_stmt_bind_result(): Number of bind variables doesn't match number of fields in prepared statement in line 30以及如何在mysqli查询中执行查询,例如mysqli_query($conn, $query)

以下是我的UPDATE查询的代码:

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_execute($stmt);
            mysqli_stmt_bind_result($stmt, $id, $updated_photo);    

            //HOW CAN I EXECUTE THE QUERY HERE?
            echo "Image Uploaded";

        }

        else {

            echo "Image is not uploaded!";

        }

}

在上面的代码中,有一条关于如何执行查询的注释行。我怎样才能做到这一点?谢谢你们

编辑:

当我点击上传按钮时,它表示图片已上传但未出现在数据库中。那是为什么?

1 个答案:

答案 0 :(得分:0)

 //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']);
  mysqli_stmt_execute($stmt);   

//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 ;
   $id=$_GET['id'];
   $stmt->execute();