3

This is a snippet of my upload php file.

Upon completion of uploading, the files upload into the correct folder (i.e.: move_uploaded_file).

However, they aren't displayed correctly in the table (only the 1st file is correct in the table).

What am I doing wrong?

    $con = mysqli_connect($theDb, $usr, $pass, "images");
    mysqli_select_db($con, "images");

//Submit button work
if(isset($_POST['submit'])){
  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];

    $selected = $_POST['tables'];

    //image type check
    if(substr($filetype, 6) == "jpeg"){

      $filepath = "categories/" . $selected . "/" . $filename; //insert in respective folders
      move_uploaded_file($filetmp,$filepath);

      $sql = "INSERT INTO `$selected` (img_name, img_path, img_type) VALUES ('$filename', '$filepath', '$filetype')";

    } 
    else{
      echo "Has to be an image!";
    }
  }
  $result = mysqli_query($con, $sql);
}

Update:

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

<?php
include '/var/db_file.php';

$con = mysqli_connect($theDb, $usr, $pass, "images");
mysqli_select_db($con, "images");

// Drop down menu
$dbname = "images";
$sql = "SHOW TABLES FROM $dbname";
$result = mysqli_query($con, $sql);
$tableNames = array();

while ($row = mysqli_fetch_row($result)) {
    $tableNames[] = $row[0];
}

echo '<br>';

echo '<select name="tables" id="tables">';
foreach ($tableNames as $name) {
    echo '<option value="' . $name . '">' . $name . '</option>';
}
echo '</select>';
// Drop down menu end

echo '<br>';

mysqli_close($con);
?>

REST OF THE CODE CONTINUES IN 1ST CODE BLOCK. Both code blocks are enclosed in the form tag

5
  • 1
    Can you show us html code of your form? Commented Jan 1, 2016 at 3:43
  • Also please show your MySQL connection code (please remember to scrub the data) Commented Jan 1, 2016 at 4:12
  • Updated with mysql connection code and html code of my form! Commented Jan 1, 2016 at 4:13
  • FYI, even if this is a practice for you, it's still a good habit to parameterize the items you are inserting. Commented Jan 1, 2016 at 4:19
  • What do you mean by parameterize? Like adding backquotes to them? Commented Jan 1, 2016 at 4:20

1 Answer 1

3
$result = mysqli_query($con, $sql);

this should be inside the for loop

so your first block code should be like this :

$con = mysqli_connect($theDb, $usr, $pass, "images");
    mysqli_select_db($con, "images");

//Submit button work
if(isset($_POST['submit'])){
  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];

    $selected = $_POST['tables'];

    //image type check
    if(substr($filetype, 6) == "jpeg"){

      $filepath = "categories/" . $selected . "/" . $filename; //insert in respective folders
      move_uploaded_file($filetmp,$filepath);

      $sql = "INSERT INTO `$selected` (img_name, img_path, img_type) VALUES ('$filename', '$filepath', '$filetype')";
      $result = mysqli_query($con, $sql);
    } 
    else{
      echo "Has to be an image!";
    }
  }

}

let me know if you have more issue

Sign up to request clarification or add additional context in comments.

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.