2013-07-19 29 views
0

我試圖在MySQL數據庫中插入多個圖像。當我在數據庫中插入單個圖像時,該程序工作得很好,但現在當我添加了3張圖像時,它給了我一個錯誤信息。它沒有給出任何MySQL錯誤消息。請檢查它。 由於無法在PHP數據庫中使用PHP插入多個圖像

不是有效的圖像

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

最新錯誤? – DevZer0

+0

*旁註:*停止使用不推薦的'mysql_ *'函數。改用MySQLi或PDO。 – Raptor

+0

它沒有閱讀else語句。它給我的信息,我回聲'如果'的聲明。 if($ image_check == false || $ image_check2 == false || $ image_check3 == false) { echo'Not a Valid Image'; } –

回答

0

您不能有相同的。嘗試將其重命名爲不同的名稱,並逐個處理它們。

即第二文件輸入image應該是image2

另外,根據以下注釋,3張圖片超過了最大上傳大小,這導致Web服務器終止POST請求。嘗試在php.ini中或通過ini_set()增加upload_max_filesize

另外,這裏有一些圖片的標題說明我張貼在這個問題的意見(其中您的代碼有潛在的問題):

  1. 停止使用過時mysql_*功能。改用MySQLi或PDO。

  2. 您的代碼受到SQL注入攻擊,因爲您直接允許將POST值插入到您的查詢中。

+0

謝謝,我已更新文件名,但仍收到相同的錯誤信息 –

+0

您是否檢查GD是否已安裝? 'getimagesize()'需要GD工作。此外,你的圖片是否有效?你應該在檢查圖像大小之前移動文件 – Raptor

+0

當我插入單個圖像時,程序工作正常,但是由於我已經開始添加多個圖像,所以它會產生問題。 –

0

您有

圖像2:輸入類型= '文件' 名稱= '圖像'>

不應認爲是

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

+0

謝謝,我已更新文件名,但仍收到相同的錯誤信息 –

相關問題