Uploading multiple images with caption – PHP – SitePoint Forums

I am trying to upload multiple images with a caption, a caption where the user can edit and update the caption.
I have no problem uploading multiple images, but I couldn’t figure out how to add a caption for each uploaded image with the edit/update option.
Image index page for upload form
Multiple Image Upload Code
$sernamename = "localhost";
$username = "root";
$passoword = "";
$databasename= "my_db";
// Create database connection
$con = mysqli_connect($sernamename, $username,$passoword,$databasename);
// Check connection
if ($con->connect_error) {
die("Connection failed". $con->connect_error);
}
// Upload multiple image in Database using PHP MYSQL
if (!empty($_FILES['multipleFile']['name'])) {
$multiplefile = $_FILES['multipleFile']['name'];
foreach ($multiplefile as $name => $value) {
$allowImg = array('png','jpeg','jpg','');
$fileExnt = explode('.', $multiplefile[$name]);
if (in_array($fileExnt[1], $allowImg)) {
if ($_FILES['multipleFile']['size'][$name] > 0 && $_FILES['multipleFile']['error'][$name]== 0) {
$fileTmp = $_FILES['multipleFile']['tmp_name'][$name];
$newFile = rand(). '.'. $fileExnt[1];
$target_dir="uploads/".$newFile;
if (move_uploaded_file($fileTmp, $target_dir)) {
$query = "INSERT INTO table_images (images) VALUES('$newFile')";
mysqli_query($con, $query);
}
}
}
}
}
Retrieve data to display submitted image from database
$query = "SELECT * FROM table_images";
$result = mysqli_query($con, $query);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$images = $row['images'];
?>
;
No Image found";
}
The image can be uploaded and displayed easily, but how can I add a caption for images that the user can edit/update.
how can i get this caption option with multiple image upload options where user can edit caption and update it
The field name must be an array, such as the field name type=’file’, the array index being the id (primary auto-increment index) corresponding to the row of the database table in which the image is. When the data is submitted, you can use a foreach() loop to get both the array index (id) and the submitted value.
Your upload file handling code MUST detect if the total size of the form data has exceeded the post_max_size parameter (the $_FILES and $_POST arrays will be empty), then it should test to ensure that each individual file has been uploaded without any errors, [‘error’] element will be a zero, before using any for uploaded file information.
Computers don’t do chance very well. Your existing code should detect and handle duplicates.
A better way to name things uniquely is to insert the row of data, get the last insert id to auto-increment from this query, and use the id in the frame by name. Since the main auto-increment index is guaranteed unique, there is no chance of duplicates.
You should also use a prepared statement whenever you supply external, unknown, and dynamic values to a query when it’s run so that special SQL characters in a value don’t break the syntax of the SQL query, that’s how SQL injection is completed. If you avoid doing this due to difficulty with the mysqli extension, switch to the much simpler PDO extension.
Edit: Another issue with upload code is that you need to display a helpful error message for each uploaded file that could not be uploaded or validated, so that the user can correct the problem and re-upload the file. or failed files.
how does this give the user the ability to edit the update caption…???
i tried to insert a form and try to update with the following code but it has no update effect
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$images = $row['images'];
$caption= $row['caption'];
$id = $row['id'];
?>
;
is the update request not affected due to ajax…???
Hard-coded values in the update request instead of post values.
I was just checking for random entry of a hardcode value…
Since the tag
If you are also using ajax to submit the caption edit/update form data, what debugging did you do to find out if any data was submitted, what values were submitted and what the server side code did ?
And, in fact, it made me see the most likely problem. When using ajax to submit a form, the submit button is not a successful form field and will not be automatically set in the form data. This is why we continually tell people to always detect if a post method form has been submitted.
Also, placing the form processing code in the middle of the form “may” work (the current code will break the output of caption forms due to reusing variable names), but the form processing code of the method post should always be above the start of the html document and in the case of using ajax, then you would need to pipe user/validation errors or success messages back to the ajax code, and terminate executing php code.
I solve it by inserting the image name in the database caption lines, but I’m not sure if it will work with the page id, I’m developing the project…
add a new page…
title field, content field other fields and finally multiple upload field
page table
insert into parent_page (field-1 field-2, field-3) values (field-1 field-2, field-3)
lastinsert->id is generated
gallery-table
insert into image-gallery (multiple images) values (multiple images) where parent_id = lastinsert->id
I will update the result once I reach this section.
Multiple images with caption are resolved without linking to other table ids…
in database i just store image name without its extension… for example pciture-1 but i want name with extension in database table like picture-1.jpg how can i get image with extension