通过输入类型文件更新blob文件

时间:2014-04-21 00:10:57

标签: php mysql sql image blob

我目前正在尝试更新数据库上的图像。文件信息正在被传输但是我得到'警告:mysql_query()期望参数1是字符串,资源在'。这是我的代码。

$size = $_FILES['file']['size'] ;       
$type = $_FILES['file']['type'];        
$tmp_name = $_FILES['file']['tmp_name'];    
$name = $_FILES['file']['name'];
mysql_select_db($database_fot, $fot);
$image = addslashes(fread(fopen($tmp_name, "rb"),$size));
$qry = "UPDATE film SET (image)" . " VALUES ('$image')";
$result=mysql_query($qry) or die(mysql_error());

我一直收到错误'你的SQL语法有错误;检查与您的MySQL服务器版本对应的手册,以便在'(图像)VALUES('ÿØÿà\ 0JFIF \ 0 \ 0 \ 0 \ 0 \ 0 \0ÿÛ\ 0“\ 0''第1行附近使用正确的语法< / p>

2 个答案:

答案 0 :(得分:0)

尝试这可能会起作用

$con = mysqli_connect("hostname", "username", "password" ,"database") or die("Could not connect database"); //the connection line
$query=mysqli_query($con,'UPDATE film SET image="'.$image.'"')or trigger_error(mysqli_error()); ///where $con is the mysqli_connect variable

注意:您应该使用mysqli而不是mysql,因为mysql不再适用于较新的mysql数据库


你应该在数据库中插入一个链接到图像而不是图像本身

答案 1 :(得分:0)

使用此脚本。

<?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;
}

    $errorinfo = $_FILES['file']["error"];
    $filename = $_FILES['file']["name"];
    $tmpfile = $_FILES['file']["tmp_name"];
    $filesize = $_FILES['file']["size"];
    $filetype = $_FILES['file']["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 film (image) VALUE (?)"))) {
            echo "Prepare failed: (" . $mysqli->errno . ")" . $mysqli->error;
        }
        $null = NULL;
        if (!$stmt->bind_param("s", $_POST['file'], $null)) {
            echo "Binding parameters failed: (" . $stmt->errno .") " . $stmt->error;
        }

        if (!$stmt->send_long_data(0, file_get_contents($_POST['file']))) {
            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();
?>