php - 无法使用 PHP 在 MySQL 数据库中插入多个图像

标签 php html mysql database image-uploading

我正在尝试在 MySQL 数据库中插入多个图像。当我在数据库中插入单个图像时,程序运行得很好,但现在当我添加 3 个图像时,它给了我一条错误消息。它没有给出任何 MySQL 错误消息。请检查一下。 谢谢

Not a Valid Image

    <!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>Untitled Document</title>
</head>

<body>
<a href="dashboard.php"> Dashboard </a>
</body>
</html>
<?php 
error_reporting(E_PARSE);  //To Remove Notices!!
global $current_id;
session_start();
if(isset($_SESSION['username']))
{


    include 'connect.php';

            $select_query=          'Select * from category';
            $select_query_run =     mysql_query($select_query);

    echo "  
        <form action='insert_product.php' method='POST' enctype='multipart/form-data' ></br>

        Product Name:   <input type='text' name='product_name'  /></br>

        Price       :   <input type= 'text' name= 'price'  /></br>

        Description :   <input type='text' name='description'  />*Seperate by Comma</br>

        Image1      : <input type='file' name= 'image' >

        Image2      : <input type='file' name= 'image' >

        Image3      : <input type='file' name= 'image3' >



                        ";



    /*------------------
    Drop Down List Start
    ------------------*/            


            echo "<select name='category'>";


            while   ($select_query_array=   mysql_fetch_array($select_query_run) )
            {

                    echo "<option value='".$select_query_array['category_id']."' >".
                    htmlspecialchars($select_query_array["name"])."</option>";


                }

         $selectTag= "<input type='submit' value='Insert'  /></select></form>";

         echo $selectTag;

    /*-----------------
    Drop Down List End
    ------------------*/    








    if(isset($_POST['product_name']) && isset($_POST['price']) && isset($_POST['description']) )
    {
         $product_name  =       $_POST['product_name'];
         $price         =       $_POST['price'];
         $description   =       $_POST['description'];
         $category      =       $_POST['category'];




    $query= "insert into products (name, price, description,  category_id ) 
                VALUES( '$product_name', $price, '$description', $category )";


    if($query_run=      mysql_query($query) )
    {

        echo 'Data Inserted';
        $current_id=     mysql_insert_id();
        //$_SESSION['current_id']= mysql_insert_id();



        }   
        else
        {
            'Error In SQL'.mysql_error();
            }
    }

    else
    {
        echo 'Plesae fill all the Fields';
        }


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


        $file   =$_FILES['image']['tmp_name'];


        if(!isset($file))
        {
            echo 'Please select an Image';

            }
            else 
            {
                $image_check=       getimagesize($_FILES['image']['tmp_name']);
                $image_check2=      getimagesize($_FILES['image2']['tmp_name']);
                $image_check3=      getimagesize($_FILES['image3']['tmp_name']);

                if($image_check==false || $image_check2==false || $image_check3==false)
                {
                    echo 'Not a Valid Image';
                    }
                    else
                    {
                        /*
                        $image          =file_get_contents ($_FILES['image']['tmp_name']    );
                        $image_name     =$_FILES['image']['name'];                      
                        $image_query    ="insert into product_images VALUES ($current_id, '$image_name', '$image')";
                        */

                        //For Image 1
                        $image      =mysql_real_escape_string(file_get_contents ($_FILES['image']['tmp_name']));
                        $image_name     =mysql_real_escape_string($_FILES['image']['name']);                      
                        $image_query    ="insert into product_images VALUES ($current_id, '$image_name', '$image')";


                        //For Image2

                        $image2=        mysql_real_escape_string(file_get_contents($_FILES['image2']['tmp_name']));
                        $image2_name=   mysql_real_escape_string($_FILES['image2']['name']);
                        $image2_query=   "insert into product_images VALUES ($current_id,'$image2_name','$image2')";

                        //For Image3
                        $image3=        mysql_real_escape_string(file_get_contents($_FILES['image3']['tmp_name']));
                        $image3_name=   mysql_real_escape_string($_FILES['image3']['name']);
                        $image3_query=  "insert into product_images VALUES ($current_id, '$image3_name', '$image3')";

                    //  $image_query=    "INSERT INTO `product_images` (`product_id`, `name`, `image`) 
                            //VALUES ('1', '{$image_name}', '{$image}')";



                        if (mysql_query($image_query) && mysql_query($image3_query) && mysql_query($image2_query))
                        {

                        //if ($image_query      =mysql_query (insert into product_images values 
                                //                          ($current_id, $image_name, $image"))




                                                            //  echo $current_id;
                                                                //echo 'Successfull';
                                                                }
                                                                else
                                                                {
                                                                    echo "<br>". mysql_error();
                                                                    }
                    }

                }
        /*-----------------
    IMAGE QUERY END
    ---------------------*/



}


else
{
    echo 'You Must Log in To View this Page!';
    }
?>

最佳答案

你不能有 2 <input type="file">与相同name 。尝试重命名为不同的名称,一一处理。

即第二个文件输入image应该是image2 .

此外,根据下面的评论,这 3 张图片超出了最大上传大小,导致 Web 服务器终止了 POST 请求。尝试增加upload_max_filesize在 php.ini 中或通过 ini_set() .

此外,这是我在问题评论中发布的一些旁注(您的代码存在潜在问题):

  1. 停止使用已弃用的 mysql_*功能。请改用 MySQLi 或 PDO。

  2. 您的代码会受到 SQL 注入(inject)攻击,因为您直接允许在查询中插入 POST 值。

关于php - 无法使用 PHP 在 MySQL 数据库中插入多个图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17740737/

相关文章:

mysql - 表 "Products"有预定义的产品,用户可以自定义价格。如何避免数据冗余?

php - 如何实现推荐系统?

javascript - 用于切换 css 模板 Bootstrap 的按钮

mysql - 如何使用虚拟实体数据预填充数据库?

php - 数据库未获取输入

html - 如何在不使用 ID 首选项的情况下更改链接的颜色?

php - 取决于用户 ID

php - 如何将 PHP 数组保存到 MySQL 数据库,以便我可以搜索数据库中的数组?

php - 为什么我的 JSON 数组查询不起作用?

html - 绝对定位的内部元素不允许在相对定位的外部元素上滚动