如何使用PHP代码将图像上传到MySQL数据库 [英] How to upload images into MySQL database using PHP code

查看:178
本文介绍了如何使用PHP代码将图像上传到MySQL数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试从HTML表单将图片储存在我的资料库中。我写了PHP代码来完成这个任务。程序不会生成任何错误消息,也不会在MySQL数据库中插入图像数据。请检查一下。
这里我正在分享我的代码片段。

  / * ---------- --------- 
IMAGE QUERY
--------------- * /


$ file = $ _ FILES ['image'] ['tmp_name'];
if(!isset($ file))
{
echo'Please select a Image';
}
else
{
$ image_check = getimagesize($ _ FILES ['image'] ['tmp_name']);
if($ image_check == false)
{
echo'不是有效图片';
}
else
{
$ image = file_get_contents($ _FILES ['image'] ['tmp_name']);
$ image_name = $ _FILES ['image'] ['name'];
if($ image_query = mysql_query(insert into product_images values(1,'$ image_name',$ image)))
{
echo $ current_id;
// echo'Successfull';
}
else
{
echo mysql_error();
}
}
}
/ * -----------------
图像查询结束
- -------------------- * /

< form action ='insert_product.php'method ='POST'enctype ='multipart / form-data'>< / br>
文件:< input type ='file'name ='image'>
< / form>




错误消息
您的SQL语法有错误;检查手册
对应于您的MySQL服务器版本正确的语法使用
在第1行附近的''



解决方案

首先,您应该检查图片列是否为BLOB类型





我们有字段 id (blob)和 (int),



因此,代码应该看起来像这样(假设ID始终为'1',让我们使用这个mysql_query):

  $ image = addslashes(file_get_contents($ _ FILES ['image'] ['tmp_name']) ); // SQL注入防御! 
$ image_name = addslashes($ _ FILES ['image'] ['name']);
$ sql =INSERT INTO`product_images`(`id`,`image`,`image_name`)VALUES('1','{$ image}','{$ image_name}');
if(!mysql_query($ sql)){//错误处理
echo出错了!:(;
}
pre>

你在许多方面都做错了,不要使用mysql函数 - 不推荐使用 PDO MySQLi 。考虑将文件存储在磁盘上使用MySQL存储图像被认为是Bad Idea™处理SQL表与大数据如图像可能有问题。



HTML表单不符合标准,应该如下所示:

 < form action =insert_product.phpmethod = POSTenctype =multipart / form-data> 
< label>文件:< / label>< input type =filename =image/>
< input type =submit/>
< / form>






Sidenote:



处理文件并将其存储为BLOB时,必须使用 mysql_real_escape_string() ,否则会导致语法错误。


I am trying to save images in my database from HTML form. I have written PHP code to accomplish this task. The program is not generating any error message, but also not inserting image data in MySQL database. Kindly check it. Here i am sharing a excerpt from my code.

        /*-------------------
    IMAGE QUERY 
    ---------------*/


    $file   =$_FILES['image']['tmp_name'];
    if(!isset($file))
    {
      echo 'Please select an Image';
    }
    else 
    {
       $image_check = getimagesize($_FILES['image']['tmp_name']);
       if($image_check==false)
       {
        echo 'Not a Valid Image';
       }
       else
       {
        $image = file_get_contents ($_FILES['image']['tmp_name']);
        $image_name = $_FILES['image']['name'];
        if ($image_query = mysql_query ("insert into product_images values (1,'$image_name',$image )"))
        {
          echo $current_id;
         //echo 'Successfull';
        }
        else
        {
          echo mysql_error();
        }
       }
   }
        /*-----------------
    IMAGE QUERY END
    ---------------------*/

    <form action='insert_product.php' method='POST' enctype='multipart/form-data' ></br>
            File        : <input type='file' name= 'image' >
    </form>

Error Message You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

解决方案

Firstly, you should check if your image column is BLOB type!

I don't know anything about your SQL table, but if I'll try to make my own as an example.

We got fields id (int), image (blob) and image_name (varchar(64)).

So the code should look like this (assume ID is always '1' and let's use this mysql_query):

$image = addslashes(file_get_contents($_FILES['image']['tmp_name'])); //SQL Injection defence!
$image_name = addslashes($_FILES['image']['name']);
$sql = "INSERT INTO `product_images` (`id`, `image`, `image_name`) VALUES ('1', '{$image}', '{$image_name}')";
if (!mysql_query($sql)) { // Error handling
    echo "Something went wrong! :("; 
}

You are doing it wrong in many ways. Don't use mysql functions - they are deprecated! Use PDO or MySQLi. You should also think about storing files locations on disk. Using MySQL for storing images is thought to be Bad Idea™. Handling SQL table with big data like images can be problematic.

Also your HTML form is out of standards. It should look like this:

<form action="insert_product.php" method="POST" enctype="multipart/form-data">
    <label>File: </label><input type="file" name="image" />
    <input type="submit" />
</form>


Sidenote:

When dealing with files and storing them as a BLOB, the data must be escaped using mysql_real_escape_string(), otherwise it will result in a syntax error.

这篇关于如何使用PHP代码将图像上传到MySQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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