Reputation: 531
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
Upvotes: 3
Views: 605
Reputation: 394
$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
Upvotes: 3