dongtun1209 2013-07-19 07:45
浏览 38
已采纳

无法使用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!';
    }
?>
  • 写回答

2条回答 默认 最新

  • doxqszx09742 2013-07-19 07:50
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 为什么我写出来的绘图程序是这样的,有没有lao哥改一下
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败