2013-07-24 96 views
0

我正嘗試將上傳的文件詳細信息存儲在我的數據庫中。我寫了下面的代碼,但我無法理解它爲什麼不讀取查詢塊。它不會生成任何MySQL錯誤消息或任何其他語法錯誤。請檢查它。無法在數據庫中存儲文件詳細信息

<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 
</head> 

<body> 
<form action="file_upload_test2.php" method="post" 
enctype="multipart/form-data"> 
<label for="file">Filename:</label> 
<input type="file" name="file" id="file"><br> 
<input type="file" name="file2" id="file"><br> 
<input type="file" name="file3" id="file"><br> 
<input type="submit" name="submit" value="Submit"> 
</form> 



</body> 
</html> 


<?php 

include 'connect.php'; 

$allowedExts = array("gif", "jpeg", "jpg", "png"); 
$extension = end(explode(".", $_FILES["file"]["name"])); 


if ((($_FILES["file"]["type"] == "image/gif") 
|| ($_FILES["file"]["type"] == "image/jpeg") 
|| ($_FILES["file"]["type"] == "image/jpg") 
|| ($_FILES["file"]["type"] == "image/pjpeg") 
|| ($_FILES["file"]["type"] == "image/x-png") 
|| ($_FILES["file"]["type"] == "image/png")) 
//&& ($_FILES["file"]["size"] < 200000) 
&& in_array($extension, $allowedExts)) 
    { 
    if ($_FILES["file"]["error"] > 0) 
    { 
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>"; 
    } 
    else 
    { 
    echo "Upload: " . $_FILES["file"]["name"] . "<br>"; 
    echo "Type: " . $_FILES["file"]["type"] . "<br>"; 
    echo "Size: " . ($_FILES["file"]["size"]/200000) . " kB<br>"; 




     $image_name=  $_FILES["file"]["name"]; 

     $path=    move_uploaded_file($_FILES["file"]["tmp_name"], 
            "upload/" . rand().$_FILES["file"]["name"]);    

      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];  

      if (mysql_query ("Insert into category_images (image_name,image_location) VALUES ('$image_name', '$path')")) 
      { 
       echo "successfull"; 
       } 
       else { 
        mysql_error(); 
        } 
     } 
    } 

else 
    { 
    echo "Invalid file"; 
    } 


?> 
+0

首先,不要使用mysql_ *,因爲它已折舊。其次,是回聲「存儲在:」迴應什麼? –

+0

好的下一次我會照顧它,是'回聲存儲在'顯示路徑;存儲在:upload/paint.png –

+0

嘗試'echo mysql_error();'顯示mysql錯誤。 – jcubic

回答

0

嘗試

$sql = "INSERT INTO `category_images` (`image_name`,`image_location`) VALUES ('".$image_name."', '".$path."')"; 

    $result = mysql_query($sql); 
    if ($result) 
    { 
      // Successful query execution 
      echo "successfull"; 
    } 
    else { 
      // Some error occured while executing query. 
      // Show some useful information using echo/print. 
      // Then stop execution after taking other necessary steps 

      die(mysql_error()); 
    } 

也,你的數據庫很容易受到SQL注入攻擊,因爲你沒有消毒的輸入。您應至少使用mysql_real_escape_string()方法來確保不會發生這種情況。

如果上述不起作用,請嘗試檢查您的連接參數是否正常以及MySQL是否正在運行。

+0

我只是將這些行粘貼到我的代碼中,並對舊的進行評論。它不會生成任何錯誤消息,但也不會顯示任何結果。 –

+0

已更新的答案。嘗試回聲$ sql,複製輸出並將其粘貼到phpmyadmin,並看看你也得到了什麼。 –

+0

它只是在數據庫中添加記錄:)並向我顯示「成功」的消息,仍試圖理解爲什麼它不工作。無論如何非常感謝:) –

相關問題