2017-06-03 97 views
0

我打算用MySQL作爲數據庫來製作一個使用PHP的圖像星系。PHP和MySQLi - 上傳多個圖像

我已經使用PHP做了多個上傳圖片,但是我遇到了問題。當我上傳8圖像在數據庫中只顯示1行數據。但在文件夾中有8張圖片我已上傳。

這裏是我的代碼:

<!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> 

<form action="a.php" method="post" enctype="multipart/form-data"> 
<input type="file" name="file_img[]" multiple> 
<input type="submit" name="btn_upload" value="Upload">  
</form> 

<?php 
include_once 'koneksi.php'; 
if(isset($_POST['btn_upload'])) 
{ 
    for($i = 0; $i < count($_FILES['file_img']['name']); $i++) 
    { 
     $filetmp = $_FILES["file_img"]["tmp_name"][$i]; 
     $filename = $_FILES["file_img"]["name"][$i]; 
     $filetype = $_FILES["file_img"]["type"][$i]; 
     $filepath = "uploads/".$filename; 

    move_uploaded_file($filetmp,$filepath); 

    $sql = "INSERT INTO files (file,path,type) VALUES ('$filename','$filepath','$filetype')"; 
    if($connect->query($sql) === TRUE) {   
header("Location: a.php");  
} else {   
header("Location: a.php");  
}  
    } 

    $connect->close();} 
?> 

</body> 
</html> 

有人能幫助我嗎?感謝您的反饋:)

+0

向我們展示您的數據庫結構'SHOW CREATE TABLE files' – Martin

+0

和'var_dump($ _ FILES)'給你什麼?這個輸出是你期望的嗎? – Martin

+0

你有沒有任何PHP錯誤? – Martin

回答

1

header location重定向移動到for循環之外。 另請參閱Upload multiple images and store their path in database

<!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> 

<form action="a.php" method="post" enctype="multipart/form-data"> 
<input type="file" name="file_img[]" multiple> 
<input type="submit" name="btn_upload" value="Upload">  
</form> 

<?php 
include_once 'koneksi.php'; 
if(isset($_POST['btn_upload'])) 
{ 
    for($i = 0; $i < count($_FILES['file_img']['name']); $i++) 
    { 
     $filetmp = $_FILES["file_img"]["tmp_name"][$i]; 
     $filename = $_FILES["file_img"]["name"][$i]; 
     $filetype = $_FILES["file_img"]["type"][$i]; 
     $filepath = "uploads/".$filename; 

    move_uploaded_file($filetmp,$filepath); 

    $sql = "INSERT INTO files (`file`,`path`,`type`) VALUES ('$filename','$filepath','$filetype')"; 
    $connect->query($sql); 

    } 
if($connect->query($sql) === TRUE) {   
header("Location: a.php");  
exit; // always add exit after header Location: 
} else {   
header("Location: a.php"); 
exit; // always add exit after header Location: 
} 
    $connect->close();} 
?> 

</body> 
</html> 
+0

你是什麼意思? –

+0

查看編輯答案 – AdhershMNair

+0

它仍然在數據庫中存儲1行數據 –

0

後您的片段答案的一些代碼美化成了顯而易見的 - 你只能得到一個新的行中的數據庫,因爲在你的for循環您重定向用戶a.php插入該行之後。 換句話說,您的for循環將始終只執行一個步驟。

<!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> 
    <form action="a.php" method="post" enctype="multipart/form-data"> 
     <input type="file" name="file_img[]" multiple> 
     <input type="submit" name="btn_upload" value="Upload"> 
    </form> 

    <?php 
    include_once 'koneksi.php'; 

    if (isset($_POST['btn_upload'])) { 
     for ($i = 0; $i < count($_FILES['file_img']['name']); $i++) { 
      $filetmp = $_FILES["file_img"]["tmp_name"][$i]; 
      $filename = $_FILES["file_img"]["name"][$i]; 
      $filetype = $_FILES["file_img"]["type"][$i]; 
      $filepath = "uploads/".$filename; 

      move_uploaded_file($filetmp, $filepath); 

      $sql = "INSERT INTO files (file, path, type) VALUES ('$filename','$filepath','$filetype')"; 
      // #NOTICE: Read about SQL Injection and why above SQL is bad. 

      // #SOLUTION: Place 1 

      if ($connect->query($sql) === true) { 
       header("Location: a.php"); 
       exit; // #NOTICE: Always add `exit` after "header Location:" 
      } else { 
       header("Location: a.php"); 
       exit; // #NOTICE: Always add `exit` after "header Location:" 
      } 
     } 

     $connect->close(); 

     // #SOLUTION: Place 2 
    } 
    ?> 
</body> 
</html> 

你現在能看到它嗎?

結帳鏈接如下!文件[1]是執行一些清理後的文件。它還包含兩條評論,標記爲#SOLUTIONPlace 1是您應該移至Place 2的代碼。移動之後,您還應該更改if條件以檢查是否所有文件都已正確上傳。另一方面,文件[2]由我編輯(可能)適合您的問題的解決方案。

我希望我能幫上忙!

https://github.com/broiniac/stackoverflow/blob/master/44346949/

編輯:@AdhershMNair: 我認爲,因爲在你的解決方案線if($connect->query($sql) === TRUE) {的,爲最後上傳的文件數據將是最後一次迭代中for環路if語句插入到數據庫兩次(一次和一次)。