Image Upload using PHP and MYSQL

Image Upload using PHP and MYSQL, PHP Image Upload

Demo Available Here



Image Upload using PHP and MYSQL download free with source code - You will learn the step-by-step process, that how to upload an image using PHP and MySQL and display an image.

 

1. Create Database and Table

 

create database demo;

//database created 

create table images (

id int NOT NULL AUTO_INCREMENT,

image varchar 100,

Primary Key (id)

);

//table created

 

2. Create HTML Form

 

< form method="POST" action="" enctype="multipart/form-data">

       < input type="file" name="image" value="" />

      < button type="submit" name="upload"> UPLOAD < /button >



< /form >

 

 

***

Give CSS or Bootstrap styling to an HTML form, if you need.

 

3. PHP Code

 

PHP Code for Upload an Image.

 

// Run the connection


$dbc = mysqli_connect("localhost","root","", "demo") 
                   or die ("could not connect to mysql");


// Now you can use the variable $dbc to connect in your queries

// If the upload button is clicked ...


if (isset($_POST['upload'])) {


    $image = $_FILES['image']['name'];

    $tmp_name = $_FILES['image']['tmp_name'];

    $folder = "images/".$image;

    // Get all the submitted data

    $sql = "INSERT into images (image) values ('$image')";

    // Execute query

    mysqli_query($dbc, $sql);

   // Move Uploaded Image in a images folder


   if (move_uploaded_file($tmp_name, $folder)) {

     $msg = "Image uploaded successfully";

     }else{

     $msg = "Failed to upload image";

   }

}

 

****

We can use more validations like a form should be not submitted empty or an image should be in a valid format.

 

PHP Code for Display an Image.

 

//Select data from table


$sql = "SELECT * from images";


// Execute query


$run = mysqli_query($dbc, $sql);

while($row=mysqli_fetch_array($run)){


$row_image = $row['image'];


echo "< img src='images/$row_image' >";


//Display images

}
Download Source Code

Latest Updates

You May Also Like


PHP Projects

PHP Data Filter between Two Dates Project with Source Code

PHP Data Filter between Two Dates - You will learn how to filter or find data between two dates in PHP/MySQL in this project.....

PHP Projects

PHP CRUD Operation Project with Source Code

PHP CRUD Operation with MySQL & Bootstrap 5 (Create, Read, Update, Delete) - We will use INSERT, SELECT, UPDATE, and DELETE statements in MySQL, which equate to CRUD capability.....

JavaScript Projects

JavaScript Weight Converter Project with Source Code

JavaScript Weight Converter Download free with source code - Simple Weight Converter App using HTML & JavaScript that converts kilograms to other measurements.....

PHP Projects

PHP Multiple Image Upload Project with Source Code

Multiple Image Upload using PHP and MySQL download free with source code - 1. Create Database and Table. 2. Create HTML Form. 3. PHP Code for Upload an Image and PHP Code for Display an Image.....

JavaScript Projects

JavaScript Stopwatch Project with Source Code

JavaScript Stopwatch Download with source code - 1. Html code for timer and buttons. 2. Javascript code for set timer, hr min, sec and start, stop and reset.....