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

无法使用PHP在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

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

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

i.e. 2nd file input image should be image2.

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().

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

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

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

You have

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

SHouldn't that be

Image2 : input type='file' name= 'image2' >