Click here to Skip to main content
15,881,173 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to insert on a multiple image on sql like on the picture below! I can only do singe image And how do I display it one by one


https://i.stack.imgur.com/RO8WX.png[^]


Inserted in a row image1 image2 image3 image4


<?php
  $msg = "";
  $msg_class = "";
  $conn = mysqli_connect("localhost", "", "", "");
  if (isset($_POST['submit'])) {
    $profileImageName = time() . '-' . $_FILES["profileImage"]["name"];
    $target_dir = "images/";
    $target_file = $target_dir . basename($profileImageName);
    if($_FILES['profileImage']['size'] > 200000) {
      $msg = "Image size should not be greated than 200Kb";
      $msg_class = "alert-danger";
    }
    if(file_exists($target_file)) {
      $msg = "File already exists";
      $msg_class = "alert-danger";
    }

    if (empty($error)) {
      if(move_uploaded_file($_FILES["profileImage"]["tmp_name"], $target_file)) {
        $sql = "INSERT INTO users SET profile_image='$profileImageName', bio='$bio'";
        if(mysqli_query($conn, $sql)){
          $msg = "Image uploaded and saved in the Database";
          $msg_class = "alert-success";
        } else {
          $msg = "There was an error in the database";
          $msg_class = "alert-danger";
        }
      } else {
        $error = "There was an erro uploading the file";
        $msg = "alert-danger";
      }
    }
  }
?>


What I have tried:

I tried anything but not of them working at all! its not inserting into sql
Posted
Updated 26-Mar-20 22:08pm

1 solution

Please respect the INSERT command syntax:
MySQL :: MySQL 8.0 Reference Manual :: 13.2.6 INSERT Statement[^]

- You did not provide the list of columns to insert
- There is no SET keyword in INSERT statement
- You did no use the VALUES clause

Moreover, you should use prepared statements to avoid SQL injection attacks:
PHP MySQL Prepared Statements[^]

SQL
if (empty($error)) {
   if(move_uploaded_file($_FILES["profileImage"]["tmp_name"], $target_file)) {
      $stmt = $conn->prepare
      (
         "INSERT INTO `users` (`profile_image`, `bio`) VALUES (?, ?)"
      );
      $stmt->bind_param("ss", $profileImageName, $bio);
      $stmt->execute();
      // ...
   }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900