Tutto Il Web

Main Menu

  • Home
  • Computer networking
  • Web development
  • Web design
  • Internet marketing
  • Web services

Tutto Il Web

Header Banner

Tutto Il Web

  • Home
  • Computer networking
  • Web development
  • Web design
  • Internet marketing
  • Web services
Web development
Home›Web development›Uploading multiple images with caption – PHP – SitePoint Forums

Uploading multiple images with caption – PHP – SitePoint Forums

By Julie R. Kelsey
July 5, 2022
0
0


esu


July 5, 2022, 4:00 p.m.


#1

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

File uploaded successfully

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


esu:

name="caption"

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.

esu:

rand()

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.


esu


July 5, 2022, 4:46 p.m.


#3

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…???

esu:

it has no update effect

esu:

UPDATE table_imagesSETlegend='hello' WHEREidentifier= 7

Hard-coded values ​​in the update request instead of post values.


esu


July 6, 2022, 12:41 p.m.


#5

I was just checking for random entry of a hardcode value…

esu:

is the update request not affected due to ajax…???

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.


esu


July 6, 2022, 2:58 p.m.


#seven

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…


esu


July 8, 2022, 5:17 p.m.


#8

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

Related posts:

  1. Daily Deal: The Modern Web Development and MySQL Programming Pack
  2. What special features of Java are useful in web development?
  3. This training collection explores advanced web development techniques
  4. The 7 Best Websites to Learn Online Web Development

Categories

  • Computer networking
  • Internet marketing
  • Web design
  • Web development
  • Web services

Recent Posts

  • What is web development and why should you pursue it?
  • Web development training focused on reaching rural Kentucky
  • The world’s leading internet marketing and design agency celebrates its 25th anniversary with a surprise announcement
  • Boost Your Business: Web Design Ideas – Chadds Ford Live
  • Step-by-step guide to launching a website

Archives

  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • February 2020
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • June 2019
  • May 2019
  • April 2019
  • March 2019
  • February 2019
  • November 2018
  • September 2018
  • June 2018
  • May 2018
  • April 2018
  • March 2018
  • February 2018
  • January 2018
  • December 2017
  • November 2017
  • June 2017
  • May 2017
  • April 2017
  • February 2017
  • August 2016
  • February 2016
  • November 2015
  • May 2015
  • January 2015
  • October 2014
  • April 2014
  • January 2014
  • September 2013
  • August 2013
  • June 2013
  • May 2013
  • March 2013
  • August 2012
  • Privacy Policy
  • Terms and Conditions