2015-04-02 37 views
0

我試圖用PHP中的表單上傳圖片,以便將信息保存到數據庫中,但圖片保存到文件夾中。我有'$ pic'變量作爲'BLOB',但是當我上傳數據庫告訴我它是0kb,並且文件夾中沒有圖片。我是很新,PHP,所以我希望你明白我的意思:d THX上傳圖像到文件夾和使用PHP/SQL的數據庫信息

下面的代碼:

<!DOCTYPE html> 
<html> 
    <head> 
     <meta charset="UTF-8"> 
     <title>Insert car!</title> 
     <link rel="stylesheet" href="css/newgame.css"/> 
    </head> 
    <body> 
    <?php 
      require_once "includes/connection.php"; 

      $target_dir = "img/"; 
      $target_file = $target_dir . basename($_FILES['pic']['name']); 

      $car = mysqli_real_escape_string($con, $_POST['car']); 
      $descr = mysqli_real_escape_string($con, $_POST['description']); 
      $manuf = mysqli_real_escape_string($con, $_POST['manuf']); 
      $dist = mysqli_real_escape_string($con, $_POST['dist']); 
      $price = mysqli_real_escape_string($con, $_POST['price']); 
      $name = mysqli_real_escape_string($con, $_POST['name']); 
      $pic = mysqli_real_escape_string($con, $_FILES['pic']['name']); 
      move_uploaded_file($_FILES["pic"]["tmp_name"], $target_file); 

      mysqli_query($con, "INSERT INTO carlist (pic, car, descr, year, dist, price, seller) VALUES ('$pic', '$car', '$descr', '$price', '$manuf', '$dist', '$name')"); 

      echo "<p>The following car information was successfully added to database:</p>"; 
      echo "<p>\"$car\"</p>"; 
      echo "<p>Description: $descr</p>"; 
      echo "<p>Price: $price</p>"; 
      echo "<p>Manufacturer: $manuf</p>"; 
      echo "<p>Picture: $pic</p>"; 
      echo "<p>Name: $name</p>"; 
      echo "<p>Distance: $dist</p>"; 

      mysqli_close($con); 

     ?> 
     <br><br> 
     <a href="newcar.html">Insert another car</a> 
     <br> 
     <a href="index.php">Home</a> 
    </body> 
</html> 
+0

請不要將圖像保存到文件服務器並將圖像路徑保存到數據庫是「更好的做法」,而不是將圖像實際存儲在數據庫中。 – 2015-04-02 09:00:06

+0

對此感到抱歉。這就是我想要做的。 – Chumppe 2015-04-02 09:17:54

+0

你可以顯示HTML你的表格 – 2015-04-02 09:20:55

回答

0

做,如果你得到任何錯誤隨意問

一些變化試試這個代碼
<!DOCTYPE html> 
<html> 
    <head> 
     <meta charset="UTF-8"> 
     <title>Insert car!</title> 
     <link rel="stylesheet" href="css/newgame.css"/> 
    </head> 
    <body> 
     <?php 
     require_once "includes/connection.php"; 

     $target_dir = "img/"; 
     $target_file = $target_dir . basename($_FILES['pic']['name']); 

     $car = mysqli_real_escape_string($con, $_POST['car']); 
     $descr = mysqli_real_escape_string($con, $_POST['description']); 
     $manuf = mysqli_real_escape_string($con, $_POST['manuf']); 
     $dist = mysqli_real_escape_string($con, $_POST['dist']); 
     $price = mysqli_real_escape_string($con, $_POST['price']); 
     $name = mysqli_real_escape_string($con, $_POST['name']); 
     $pic = mysqli_real_escape_string($con, $_FILES['pic']['name']); 


     mysqli_query($con, "INSERT INTO carlist (pic, car, descr, year, dist, price, seller) VALUES ('$pic', '$car', '$descr', '$price', '$manuf', '$dist', '$name')"); 
     //Writes the photo to the server 
     if (move_uploaded_file($_FILES['pic']['tmp_name'], $target)) { 

//Tells you if its all ok 
      echo "The file " . basename($_FILES['pic']['name']) . " has been uploaded, and your information has been added to the directory"; 
     } else { 

//Gives and error if its not 
      echo "Sorry, there was a problem uploading your file."; 
     } 

     echo "<p>The following car information was successfully added to database:</p>"; 
     echo "<p>\"$car\"</p>"; 
     echo "<p>Description: $descr</p>"; 
     echo "<p>Price: $price</p>"; 
     echo "<p>Manufacturer: $manuf</p>"; 
     echo "<p>Picture: $pic</p>"; 
     echo "<p>Name: $name</p>"; 
     echo "<p>Distance: $dist</p>"; 

     mysqli_close($con); 
     ?> 
     <br><br> 
     <a href="newcar.html">Insert another car</a> 
     <br> 
     <a href="index.php">Home</a> 
    </body> 
</html> 
+0

謝謝你的答案..我實際上通過從$ target_file變量中刪除'basename'並將數據庫中的BLOB更改爲TEXT來修復它。然後我獲取了tmp_name上傳它到文件夾和名稱到數據庫。我上面的描述有點令人困惑,我很抱歉,我希望文件(圖片)保存到一個文件夾中,圖片名稱保存在數據庫中,然後通過數據庫發佈。 – Chumppe 2015-04-02 11:16:32

相關問題