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条)

报告相同问题?

悬赏问题

  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟