How to add multiple images into database using mysql and php

[et_pb_section fb_built=”1″ theme_builder_area=”post_content” _builder_version=”4.27.2″ _module_preset=”default”][et_pb_row _builder_version=”4.27.2″ _module_preset=”default” theme_builder_area=”post_content”][et_pb_column _builder_version=”4.27.2″ _module_preset=”default” type=”4_4″ theme_builder_area=”post_content”][et_pb_text _builder_version=”4.27.2″ _module_preset=”default” theme_builder_area=”post_content” text_font=”|700|||||||” text_text_color=”#000000″ hover_enabled=”0″ sticky_enabled=”0″]

// start multiple image

$stmt = $conn->prepare(“INSERT INTO cat_images (img_url, cat_id) VALUES (?, ?)”);
$stmt->bind_param(“si”, $imagePath, $cat_id);
$uploadedImages = $_FILES[‘images’];

$cat_id = $conn->insert_id;

foreach ($uploadedImages[‘name’] as $key => $value) {
$targetDir = “main-images/galleries/”;
$fileName = basename($uploadedImages[‘name’][$key]);
$targetFilePath = $targetDir . $fileName;
if (file_exists($targetFilePath)) {
echo “Sorry, file already exists.<br>”;
} else {
if (move_uploaded_file($uploadedImages[“tmp_name”][$key], $targetFilePath)) {
$imagePath = $targetFilePath;
$stmt->execute();
echo “The file ” . $fileName . ” has been uploaded successfully.<br>”;
} else {
echo “Sorry, there was an error uploading your file.<br>”;
}
}
}
$stmt->close();
$conn->close();

// end multiple image

[/et_pb_text][/et_pb_column][/et_pb_row][/et_pb_section]

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top