Mysqli prepare语句中断将图像插入数据库 [英] Mysqli prepare statement breaks image insert into database

查看:50
本文介绍了Mysqli prepare语句中断将图像插入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个运行良好的sql语句,直到尝试将其更改为sql预处理语句为止.

I have a sql statement that worked fine until I attempted to change it to a sql prepared statement.

这是旧的插入物:

$sql = "INSERT INTO items (seller, post_date, expiration_date, image, description, name, category, startBid, buyPrice, minPrice, sold) VALUES ('$id_user', NOW(), '$postDate', '$image', '$description', '$itemName', 0, '$startBid', '$buyNow', '$reservation', 0)";
$db->send_sql($sql);

我试图在这里准备它:

$stmt = $mysqli->prepare("INSERT INTO items (seller, post_date, expiration_date, image, description, name, category, startBid, buyPrice, minPrice, sold) VALUES (?, NOW(), ?, ?, ?, ?, ?, ?, ?, ?, 0)";
$stmt->bind_param("isbssiddd", $id_user, $postDate, $image, $description, $itemName, $itemCategory, $startBid, $buyNow, $reservation);
$stmt->execute();
$stmt->close();

这两个语句都执行,但是它们在数据库中导致不同的图像值.第一条语句的图像值是我期望的,并且可以检索/显示.用prepared语句放入的图像显示了放入数据库的内容,但未显示为有效图像.图像字段是长号.我要去哪里错了?谢谢!

Both statements execute but they result in different image values in the database. The image value of the first statement is what I expected and can retrieved/shown. The image put in with the prepared statement shows stuff put in the database but does not show up as a valid image. The image field is a longblob. Where am I going wrong? Thanks!

推荐答案

找出了问题所在.这就是我获取$ image的方式:

Figured out the issue. This is how I was getting my $image:

if (!empty($_FILES['inputPic']['tmp_name']))
{
    if ($_FILES['inputPic']['type'] == "image/jpeg" || $_FILES['inputPic']['type'] == "image/jpg" || $_FILES['inputPic']['type'] == "image/png")
    {
        if ($content = file_get_contents($_FILES['inputPic']['tmp_name']))
        {
            $image = addslashes($content);
        }
    }
}

我需要在旧的mysql语句中使用addslashes函数,但现在准备时不需要.将其设置为$image = file_get_contents($_FILES['inputPic']['tmp_name'])可解决该问题

I needed the addslashes function in the old mysql statement but not when it is prepared now. Making it $image = file_get_contents($_FILES['inputPic']['tmp_name']) resolved the issue

这篇关于Mysqli prepare语句中断将图像插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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