Uploading multiple images with caption – PHP – SitePoint Forums

0

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, like 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.

Share.

Comments are closed.