无法使用PHP在MySQL数据库中插入多个映像 [英] Unable to Insert Multiple images in MySQL Database using PHP

查看:142
本文介绍了无法使用PHP在MySQL数据库中插入多个映像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to insert multiple images in MySQL database. The program was working perfectly fine when I was inserting a single image in database, but now when I have added 3 images, its giving me an error message. Its not giving any MySQL error message. Kindly check it. Thanks


不是有效图片

Not a Valid Image


$ b b

    <!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 相同。尝试将其重命名为不同的名称,并逐个处理它们。

You cannot have 2 <input type="file"> with same name. Try to rename it to different names, and handle them one by one.

第二个文件输入图像应为 image2

另外,基于以下评论,3个图像超过最大上传大小,这导致web服务器终止POST请求。尝试增加php.ini或 ini_set()中的 upload_max_filesize

In additionally, based on the comments below, the 3 images exceed the maximum upload size, which results the web server terminated the POST request. Try to increase the upload_max_filesize in php.ini or via ini_set().

此外,这里有一些sidenotes我张贴在问题的评论(您的代码有潜在的问题):

Also, here are some sidenotes I posted in the question comments (which your codes have potential problems):


  1. 使用已弃用的 mysql _ * 函数停止。使用MySQLi或PDO代替。

  1. stop using deprecated mysql_* functions. use MySQLi or PDO instead.

您的代码会遭受SQL注入攻击,因为您直接允许在您的查询中插入POST值。

your code is subjected to SQL Injection attack, as you directly allow POST values to be inserted in your query.

这篇关于无法使用PHP在MySQL数据库中插入多个映像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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