How To Insert Multiple Image in PHP MySQL

How To Insert Multiple Image in PHP MySQL

Good day! Today, I will tackle How to Insert Multiple Image in PHP MySQL. I hope that this topic would help you especially those newbie programmers who find it difficulty in inserting image MySQL Database using PHP.

This PHP Tutorial with source code is the best answer to solve the problem raised in other sites.

If you are looking for more complete BEST PHP PROJECTS WITH SOURCE, you can jump in here. For sure you will learn so much from these materials.

Steps How to Insert Multiple Image in PHP MySQL

Step 1. Database Setup

In this step, we are going to execute a query that will create a table that will serve as storage for our images. This table named “tbl_images”.

CREATE TABLE `tbl_images` (
	`id` INT(11) NOT NULL AUTO_INCREMENT,
	`location` VARCHAR(100) NOT NULL,
	PRIMARY KEY (`id`)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB
AUTO_INCREMENT=12;

Step 2. Setting Up Connection

In this step, we will create a connection between PHP and MySQL.

Here’s the following code.

<?php
$host = "localhost";
$user = "root";
$password = "";
$database = "db_name";
	$conn = new mysqli($host, $user, $password, $database);
        if($conn->connect_error){
            die("FAILED:" . $conn->connect_error);
        }  
?>

 

Step 3. Create index.php file

In index.php file, it will allow the user to select the image. Here’s the following code.

 <form method="POST" action="insert.php" enctype="multipart/form-data">
        <input type="file" name="files[]" multiple>
        <input type="submit" name="submit" value="ADD">
    </form>

Step 4. Create insert.php file

In this file, it will insert your image location to your database.

<?php 
 $output = '';  
 if(is_array($_FILES))   
 {  
      foreach ($_FILES['files']['name'] as $name => $value)  
      {  
           $file_name = explode(".", $_FILES['files']['name'][$name]);  
           $allowed_ext = array("jpg", "jpeg", "png", "gif");  
           if(in_array($file_name[1], $allowed_ext))  
           {  
                $new_name = md5(rand()) . '.' . $file_name[1];  
                $sourcePath = $_FILES['files']['tmp_name'][$name];  
                $targetPath = "filelocation/".$new_name;  
                $sql = "INSERT INTO tbl_images (id, location) VALUES (null, '{$targetPath}')";
                $image = mysqli_query($conn,$sql);
                if(move_uploaded_file($sourcePath, $targetPath))  
                {  
                }                 
           }            
      }  

 }  
 ?>
 
            alert ("Successfully added.");
            window.location = "index.php";

Step. 5 Finally, create a folder named filelocation for the destination of the image.
End.

Articles you may like:

If you desire to create a Database design for your project, you may be like the list of database design projects with complete documentation below.

For More Database Design Examples

Inquiries

If you have any questions or suggestions about inserting multiple images in PHP MySQL. please feel free to contact me on our contact page.

Leave a Comment