I am working on a photo uploader that allows me to upload multiple images at one time to create photo galleries. This part works fine.
What I want to do is record the filename, active status and created timestamp of each image into a database table called photos
using a MySQLi prepared statement, and I want each photo's filemane (in this case) stored in its own row. I want to do this in one shot, not wrapping the query in a loop. Each gallery will not always have the same amount of photos.
My initial results only stored the first filename. After about 4 hours of research today, I settled on this code, which works exactly as I want it to, yet there is a nagging feeling that this method is inefficient. Is this creating separate insert queries for each photo's information? For reference, the $names
array is created by the uploading script and only contains the filename, $active
is a hard-coded variable that can be changed through an update page elsewhere in this system.
$sql = 'INSERT INTO photos (photo_filename, photo_active, photo_created)
VALUES (?, ?, NOW())';
$stmt->prepare($sql);
foreach ($names as $filename) {
$stmt->bind_param('si', $filename, $active);
$stmt->execute();
}
$imageOK = $stmt->affected_rows;
If this code is in fact creating multiple insert queries, any help on how to insert a group of records (could be 25, could be 75 or more) in one single query would be greatly appreciated. This is for a back-end system that only I will be using, so the server load will not be great. Still, if the above code is not the best way to do business, then I want to learn the best way.
The desired result this code is providing:
+----------------+--------------+---------------------+
| photo_filename | photo_active | photo_created |
+----------------+--------------+---------------------+
| image_1.jpg | yes | 2015-06-25 15:12:47 |
+----------------+--------------+---------------------+
| image_2.jpg | yes | 2015-06-25 15:12:47 |
+----------------+--------------+---------------------+
and so on...
Many thanks!