PHP和mysqli:使用表单将blob上传到数据库

PHP和mysqli:使用表单将blob上传到数据库,php,html,mysqli,blob,Php,Html,Mysqli,Blob,我正在尝试使用PHP和mysqli将图像作为blob上传到数据库中。如果我只是尝试使用查询插入图像名称,它工作正常,并创建一个具有该名称的新行(blob为null)。一旦我开始尝试使用query和jquery函数通过表单将blob上传到数据库中,就会创建一个新行,但blob显示为0b。第一位代码是html表单。第二个是提交表单后调用的php文件。如有任何建议,将不胜感激。提前谢谢 澄清:我知道将图像上传到目录是一种更有效的存储方式。为了理解如何使用其他选项,我正在尝试弄清楚如何使用blob 编辑

我正在尝试使用PHP和mysqli将图像作为blob上传到数据库中。如果我只是尝试使用查询插入图像名称,它工作正常,并创建一个具有该名称的新行(blob为null)。一旦我开始尝试使用query和jquery函数通过表单将blob上传到数据库中,就会创建一个新行,但blob显示为0b。第一位代码是html表单。第二个是提交表单后调用的php文件。如有任何建议,将不胜感激。提前谢谢

澄清:我知道将图像上传到目录是一种更有效的存储方式。为了理解如何使用其他选项,我正在尝试弄清楚如何使用blob

编辑

为了澄清,我还尝试了以下代码片段

if ($filetype == "image/jpeg" && $filesize > 0 && $filesize < 1048576) {
        echo "Import of photo success";

        $aimage = file_get_contents($tmpfile);

        if (!($stmt=$mysqli->prepare("INSERT INTO actor_info (aname, aimage) VALUE (?,?)"))) {
            echo "Prepare failed: (" . $mysqli->errno . ")" . $mysqli->error;
        }

        if (!$stmt->bind_param("sb", $_POST['aname'], $aimage)) {
            echo "Binding parameters failed: (" . $stmt->errno .") " . $stmt->error;
        }

        if (!$stmt->execute()) {
            echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
        }

        else {
            printf("%d row inserted.<br/>", $stmt->affected_rows);
        }
    }
if($filetype==“image/jpeg”&&$filesize>0&&$filesize<1048576){
echo“导入照片成功”;
$aimage=file\u get\u contents($tmpfile);
if(!($stmt=$mysqli->prepare(“插入参与者信息(aname,aimage)值(?,)”){
echo“准备失败:(“$mysqli->errno.”“$mysqli->error;
}
if(!$stmt->bind_参数(“sb”、$\u POST['aname']、$aimage)){
echo“绑定参数失败:(“$stmt->errno.”“$stmt->error;
}
如果(!$stmt->execute()){
echo“执行失败:(“$stmt->errno.”“$stmt->error;
}
否则{
printf(“%d行插入。
,$stmt->受影响的行); } }
结束编辑

错误消息

导入照片成功 注意:第43行未定义索引:aimage in/nfs/stak/students/m/martalic/public_html/CS494/Test/addActorInfo.php

警告:file_get_contents()[function.file get contents]:第43行的/nfs/stak/students/m/martalic/public_html/CS494/Test/addActorInfo.php中的文件名不能为空 插入1行

HTML表单

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
<form id="form" method="post" action=addActorInfo.php enctype="multipart/form-data">
    Actor Name: <input id="aname" type="text" name="aname" class="required" maxlength="64"><br><br>
    Attach Photo: <input id="aimage" type="file" name="aimage" class="required"><br><br>
    <input type="submit" name="ADD" value="ADD"/>
</form>

</body>
</html>

演员姓名:

附上照片:

PHP

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
Actor Information
<?php
ini_set('display_errors', 'On');
ini_set('display_startup_errors', 'On');
error_reporting(E_ALL);

$mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}

if(isset( $_POST["ADD"]) ) {
    $aname = $_POST['aname'];
    $errorinfo = $_FILES["aimage"]["error"];
    $filename = $_FILES["aimage"]["name"];
    $tmpfile = $_FILES["aimage"]["tmp_name"];
    $filesize = $_FILES["aimage"]["size"];
    $filetype = $_FILES["aimage"]["type"];

    if (!($filetype == "image/jpeg" && $filesize > 0)) {
        echo "Import of photo failed";
    }

    if ($filetype == "image/jpeg" && $filesize > 0 && $filesize < 1048576) {
        echo "Import of photo success";

        if (!($stmt=$mysqli->prepare("INSERT INTO actor_info (aname, aimage) VALUE (?,?)"))) {
            echo "Prepare failed: (" . $mysqli->errno . ")" . $mysqli->error;
        }
        $null = NULL;
        if (!$stmt->bind_param("sb", $_POST['aname'], $null)) {
            echo "Binding parameters failed: (" . $stmt->errno .") " . $stmt->error;
        }

        if (!$stmt->send_long_data(0, file_get_contents($_POST['aimage']))) {
            echo "Did not get contents";
        }

        if (!$stmt->execute()) {
            echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
        }

        else {
            printf("%d row inserted.<br/>", $stmt->affected_rows);
        }
    }
    else {
        echo "Image must be under 1 MB";
    }
    $stmt->close();
}
$mysqli->close();
?>
</body>
</html>

演员信息

您是否考虑过像以前那样上传图像,然后将其放入文件夹/uploadedimg/中,然后引用文件名(随机生成)并将其插入数据库?BLOB并不是真正有效的

所以上传。文件名。保存文件。在db中引用文件imgName=myfile123.png,然后在需要时使用它

$myvar = $SQLQuery;

<img src="/uploadedimg/". $myvar ." " alt="my image" />
$myvar=$SQLQuery;

在我的绑定参数中发现问题。通过从“b”切换到“s”,我可以将图像作为一个blob存储在数据库中。

绝对可以。我正在尝试使用blob来了解它的工作原理。忘了blob的事情吧,看起来你有正确的上传代码,只需记下你上传它的位置,并将文件名作为VARCHAR放入数据库。不要让BLOB让你失望。谢谢你的建议,但我有兴趣学习BLOB如何上传和存储。我可能会也可能永远不会再使用它,但至少我会理解它的优点和缺点,因为我已经尝试过了。