2017-06-03 67 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语句插入到数据库两次(一次和一次)。