简体   繁体   English

将blob类型的图像上传到数据库,我的代码说它正在上传图像,但没有存储在数据库中

[英]Uploading blob type images to database, my code says it is uploading the image but not being stored in the database

This is my code below, the HTML form at the top is a simple form to upload an image and the SQL commands within the PHP is for uploading to the database. 这是我下面的代码,顶部的HTML表单是上传图像的简单表单,PHP中的SQL命令用于上传至数据库。 The problem I am having is it appears to be accessing the if(substr($imageType,0,5) == "image") and outputting "Image uploaded" to the browser but not actually uploading this image to my database. 我遇到的问题是它似乎正在访问if(substr($ imageType,0,5)==“ image”)并将“ Image Uploaded”输出到浏览器,但实际上并未将此图像上传到我的数据库。

My database table named blob has three fields; 我的数据库表名为blob,具有三个字段; id (int(11) also auto increment and primary key), name (varchar(30)) and image (blob). id(int(11)以及自动递增和主键),名称(varchar(30))和图像(blob)。

Edit: I tried the backticks suggested by M Miller but no luck, using the mysql_error() gave me this error 编辑:我尝试了由米勒建议的反引号,但是没有运气,使用mysql_error()给了我这个错误
Warning: mysql_error() expects parameter 1 to be resource, boolean given in C:\\wamp\\www\\Box\\index.php on line 31 警告:mysql_error()期望参数1为资源,在第31行的C:\\ wamp \\ www \\ Box \\ index.php中给出布尔值

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Image Upload</title>
</head>

<body>

<form action="index.php" method ="POST" enctype="multipart/form-data">
    <input type="file" name="image" /><input type="submit" name="submit" value="Upload" />
</form>

<?php

if(isset($_POST['submit']))
{
    mysql_connect("localhost", "root", "root");//password root
    mysql_select_db("content");//db name content

    $imageName = mysql_real_escape_string($_FILES["image"]["name"]);//provides protection against injection
    $imageData = mysql_real_escape_string(file_get_contents($_FILES["image"]["tmp_name"]));

    $imageType = mysql_real_escape_string($_FILES["image"]["type"]);

    if(substr($imageType,0,5) == "image")
    {
        mysql_query("INSERT INTO 'blob' VALUES('','$imageName','$imageData')");
        echo "Image Uploaded";
    }
    else
    {
        echo "Only images are allowed";
    }

}


?>

</body>
</html>

Similar to BarryDevSF's solution by changing the line to read: 与BarryDevSF的解决方案类似,将行更改为:

mysql_query("INSERT INTO `blob` VALUES(NULL,'$imageName','$imageData')");

instead of: 代替:

mysql_query("INSERT INTO 'blob' VALUES('','$imageName','$imageData')");

solved the issue. 解决了这个问题。

You're passing an empty string to the INT id. 您正在将一个空字符串传递给INT ID。 I would try it without that. 如果没有它,我会尝试的。

instead of: 代替:

mysql_query("INSERT INTO 'blob' VALUES('','$imageName','$imageData')");

try: 尝试:

mysql_query("INSERT INTO blob (name, image) VALUES('$imageName','$imageData')");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM