0

I know that this code below work perfectly, I already tried it and it worked fine for one input type "file", but what if I have 7 inputs type "file"? how to check if one of them is empty or all of them are empty to not upload blank values to the database? thanks in advance.

if ((!($_FILES['image']['name']))) /* If there Is No file Selected*/ {

"Upload SQL status"

} else /* If file is  Selected*/ {

"Upload SQL status"

$image = $_FILES['image']['name'];
move_uploaded_file($img,"images/$image");
}

Such as:

<input  type="file" accept="image/x-png,image/jpeg,image/jpg,image/png" 
 name="image1" />


<input  type="file" accept="image/x-png,image/jpeg,image/jpg,image/png" 
 name="image2" />


<input  type="file" accept="image/x-png,image/jpeg,image/jpg,image/png" 
 name="image3" />


<input  type="file" accept="image/x-png,image/jpeg,image/jpg,image/png" 
 name="image4" />


<input  type="file" accept="image/x-png,image/jpeg,image/jpg,image/png" 
 name="image5" />


<input  type="file" accept="image/x-png,image/jpeg,image/jpg,image/png" 
 name="image6" />


<input  type="file" accept="image/x-png,image/jpeg,image/jpg,image/png" 
 name="image7" />

Getting the image from the field:

    //getting the image from the field
    $image1 = $_FILES['image1']['name'];


    //getting the image from the field
    $image2 = $_FILES['image2']['name'];


    //getting the image from the field
    $image3 = $_FILES['image3']['name'];


    //getting the image from the field
    $image4 = $_FILES['image4']['name'];


    //getting the image from the field
    $image5 = $_FILES['image5']['name'];

    //getting the image from the field
    $image6 = $_FILES['image6']['name'];

    //getting the image from the field
    $image7 = $_FILES['image7']['name'];

After click update, I want my other fields also to be updated in the database like this:

$update_restaurant = "update restaurants set 
restaurant_name='$name',restaurant_time='$time', 
area='$area',cuisine='$cuis',header='$header', 
Overview='$overview',Menu='$menu',website='$wb', 
facebook='$fb',meals='$meals',payment='$pay', phone='$phone' , 
map='$map_link', maptext='$map_des', img1='$image1', img2='$image2', 
img3='$image3', img4='$image4',img5='$image5', img6='$image6', 
img7='$image7', mapid='$map_id' where restaurant_id='$update_id'";

Thanks.

2 Answers2

1

Use name="image[]" instead of name="image1", name="image2" etc.

Then, you can iterate with field like follows:

foreach($_FILES['image'] as $file) {
    if (is_uploaded_file($file)) {
        // file was uploaded - do something

    }
}
Silvio Delgado
  • 6,798
  • 3
  • 18
  • 22
  • Ok, greatttt, I will try this out but how the database will add each picture in their specific column in the table since they all have the same name? I edited my code in my question to check it out. Thank you so much appreciate it. – Jukebox Music Apr 13 '19 at 18:34
1

You're updating a single row in your database, you should probably be inserting each image as a new row.

e.g.

$stmt = $pdo->prepare("insert into restaurants set restaurant_name=:name, filename=:name, ...");
$stmt->execute([$restaurantName, $img1]);

(standard injection warning applies)

Any time you have column names like img1, img2, img3... you're probably doing something wrong. Even two repeated columns like phone1 and phone2 is probably going to come back to bite you sooner or later when some special snowflake client decides they really, really, really need a 3rd phone number.

mpen
  • 272,448
  • 266
  • 850
  • 1,236