2013-07-19 44 views
0

我正在創建一個PHP程序,它將產品信息及其圖像作爲輸入並將信息存儲在數據庫中。我寫了下面的代碼,它在數據庫中插入'產品信息'記錄,但沒有在數據庫中插入'圖像記錄',並且還給我一個錯誤消息。請檢查它並告訴我我犯了什麼錯誤。 謝謝。無法在MySQL中插入圖像記錄表

您的SQL語法錯誤;檢查對應於你的MySQL服務器版本正確的語法在1號線

<!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> 
</body> 
</html> 
<?php 
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> 

     File  : <input type='file' name= 'image' > 


         "; 



    /*------------------ 
    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(); 



     } 
     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']); 

       if($image_check==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)"; 


        // $image_query= "INSERT INTO `product_images` (`product_id`, `name`, `image`) 
          //VALUES ('1', '{$image_name}', '{$image}')"; 


         if (mysql_query($image_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!'; 
    } 
?> 

回答

1

兩個問題使用「」附近的手冊。

首先,您應該轉義值以防止SQL注入,並且還要處理$image包含二進制數據這一事實。

其次,您必須在SQL中引用$image

試試這個:

$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')"; 
+0

非常感謝你!它解決了這個問題:) –

0

嘗試使用它插入SQL是這樣的:

$query= "insert into products (name, price, description, category_id) 
       VALUES('$product_name', '$price', '$description', $category)"; 


$價格必須引號內太.....