如何使用 mysqli 准备好的语句在数据库中更新? [英] How to UPDATE in database using mysqli prepared statement?

查看:50
本文介绍了如何使用 mysqli 准备好的语句在数据库中更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前使用 mysqli 进行查询,现在我将其转换为 mysqli 准备好的语句.我正在尝试使用上传图片更新特定数据,但我不知道为什么会出现错误

I'm using mysqli before to my query and now I convert it to mysqli prepared statements. 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():绑定变量的数量与第 30 行准备好的语句中的字段数量不匹配

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)

Also, how can I execute the query in mysqli query like mysqli_query($conn, $query)

以下是我的 UPDATE 查询代码:

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_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!";

        }

}

在上面的代码中,有一条关于如何执行查询的注释行.我该怎么做?

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

当我单击上传"按钮时,它说图片已上传但未出现在数据库中.这是为什么?

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

推荐答案

对于程序方式

$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);

试试面向对象的风格

$query = "UPDATE `crew_info` SET `updated_photo` = ? WHERE `id` = ?";
$stmt = $conn->prepare($query);
$stmt->bind_param("si", $imageData, $_GET['id']);
$stmt->execute();

这篇关于如何使用 mysqli 准备好的语句在数据库中更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆