0

I am planning to make an image galary using PHP with MySQL as a database.

I already make a multiple upload images using PHP but I got a problem. When I uploaded 8 images in database only show 1 row data. But in folder there are 8 images that I have upload.

Here is my code :

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

Can someone help me ? Thanks for ur feedback :)

5
  • Show us your database structure with SHOW CREATE TABLE files Commented Jun 3, 2017 at 18:18
  • and what does var_dump($_FILES) give you? Is that output what you expect? Commented Jun 3, 2017 at 18:19
  • And do you have any PHP errors? Commented Jun 3, 2017 at 18:19
  • id(int 11) PK AI, file (varchar 250), path (varchar 100), type (varchar 100). No error in my php code Commented Jun 3, 2017 at 18:24
  • Please edit your question and put the details in the question. thanks Commented Jun 3, 2017 at 18:30

2 Answers 2

1

Move the header location redirect outside the for loop. Also Refer 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>
Sign up to request clarification or add additional context in comments.

3 Comments

what do u mean ?
See the Edited Answer
it's still store 1 row data in DB
0

After some code beautification of your snippet answer became obvious - you only get one new row in database, because in your for loop you redirect user to a.php right after inserting this row. In other words your for loop will always perform one and only one step.

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

Can you see it now?

Checkout link below! File [1] is your file after performing some cleaning. It's also contains two comments marked as #SOLUTION. Place 1 is the code you should move to Place 2. After moving you should also change if condition to check if all files were uploaded correctly. File [2] on the other hand is edited by me with (probably) proper solution for your problem.

I hope I helped!

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

EDIT: @AdhershMNair: I think that because of line if($connect->query($sql) === TRUE) { in your solution, data for last uploaded file will be inserted into database twice (once for last iteration in for loop and once for if statement).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.