Upload Image in PHP MySQL Database and display

In this tutorial, I am going to explain the process of how to Upload image in PHP MySQL Database and display that image from the database and store it in the project folder.

First of all, we have to create a table in the database and then we can insert the image in that particular table in the database.

After inserting the image into the database, it will be moved to the public folder as well so that we can retrieve the image later on.

Also Read, CRUD Operation in PHP using ajax jquery

How to Upload Image in PHP and Store in Database and Folder
 

Here I am using a table ‘image’ with fields like name, age, and image.

DDL information of the table:-

CREATE TABLE `image` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(225) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `age` varchar(225) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `image` varchar(225) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci

Complete Code to Upload Image in PHP MySQL Database and display

<?php
include('connect.php');
//insert data into database
if(isset($_POST['save'])){
    $name=$_POST['name'];
	$age=$_POST['age'];
	$target="images/".basename($_FILES['image']['name']);
	$sql="INSERT INTO image(name,age,image)VALUES('$name','$age','$target')";
	$stmt=$con->prepare($sql);
	$stmt->execute();
	move_uploaded_file($_FILES['image']['tmp_name'],$target);
	echo "<div class='alert alert-success' role='alert'>Submitted Successfully</div>";
}
//fetch the data from table
    $stmt2 = $con->prepare("select * from image order by id DESC");
    $stmt2->execute();
    $details = $stmt2->fetchAll(PDO::FETCH_ASSOC);
?>
<html>
<head>
<title>image example</title>
<link rel="stylesheet" href="bootstrap.css" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="bootstrap-theme.css" crossorigin="anonymous">
<style>
.container{
	width:50%;
	height:30%;
	padding:20px;
}
</style>
</head>
<body>
	<div class="container">
	<h3><u>Upload image and insert into database in PHP</u></h3>
	<br/>
	<form action="" method="post" enctype="multipart/form-data">
			<div class="row">
				<div class="col-sm-8">
					<div class="form-group">
					    <label for="name">Name:</label>
					    <input type="text" class="form-control" name="name">
					    
					</div>
				</div>
			</div>
			<div class="row">
				<div class="col-sm-8">
					<div class="form-group">
					    <label for="name">Age*:</label>
					    <input type="number" class="form-control" name="age">
					    
					</div>
				</div>
			</div>
			<div class="row">
				<div class="col-sm-8">
					<div class="form-group">
					    <label for="name">Image*:</label>
					    <input type="file" class="form-control" name="image">
					    
					</div>
				</div>
			</div>
			<br/><br/>
			  <button type="submit" class="btn btn-primary" name="save">Submit</button>
		</form>
		<br/><br/>
		<h3>View Details</h3><br/>
		<table class="table table-bordered">
		    <thead>
		      <tr>
		        <th>Name</th>
		        <th>Age</th>
		        <th>Image</th>
		      </tr>
		    </thead>
		    <tbody>
   <?php
		    foreach($details as $detail){
				if($detail){
					?>
					<tr>
		        	<td><?php echo $detail['name'];?></td>
		        	<td><?php echo $detail['age'];?></td>
		        	<td><img src="<?php echo $detail['image']?>" width="200" height="150" alt="photo"></img></td>
		      </tr>
		    <?php
			}
				else{
					?>
					<tr>
						<td colspan="3" style="text-align: center">Not available</td>
					</tr>
					<?php
				}
			}
	?>
		</tbody>
	  </table>
</div>
<script src="jquery-3.2.1.min.js"></script>
<script src="bootstrap.min.js"></script>
</body>
</html>

Connect.php:-

<?php
$servername='localhost';
$username="root";
$password="";

try
{
	$con=new PDO("mysql:host=$servername;dbname=blog",$username,$password);
	$con->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
	//echo 'connected';
}
catch(PDOException $e)
{
	echo '<br>'.$e->getMessage();
}
	
?>

Download the bootstrap CSS and js files from google and include the path of the files in the href attribute of the link tag and src attribute of the script tag respectively.

Also read, Log in and Log out using session in PHP and MySQLi

CONCLUSION:- I hope this article will help you to upload images in PHP and store it in the database and folder. If you have any doubt then please leave your comment below.

1 thought on “Upload Image in PHP MySQL Database and display”

  1. I have been exploring for a little bit for any high-quality articles or blog posts on this kind of house .

    Exploring in Yahoo I at last stumbled upon this web site.
    Reading this information So i am happy to express
    that I have a very excellent uncanny feeling I found out exactly what I needed.
    I most undoubtedly will make sure to do not put out of your mind this
    site and provides it a glance regularly.

    Reply

Leave a Comment

KUPU178

thor311

thor311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

HOKI311

HOKI311

sbobet WAP

hoki311

sabung ayam online

hoki311

hoki311

live casino online

HOKI311

hoki311

KUPU178

KUPU178

KUPU178

KUPU178

KUPU178

mix parlay sbobet

hoki311

hoki311

hoki 311

togel hoki311

hoki311

THOR311

THOR311

THOR311

ceri188

ceri188

ceri188

ratu311 slot

THOR311

kupu178 toto hongkong

hoki311

hoki311

SABA Sport

sbobet

hoki311

THOR311

RATU311

kupu178 togel online

kupu178 slot demo

slot ratu311

pola slot gacor

ratu311

live casino online

hoki311

slot gacor online

hoki311

mix parlay

hoki311

sabung ayam online

hoki311

THOR311

THOR311

HOKI311

SBOBET WAP

Judi Bola Online

hoki311

hoki311

slot gacor

sabung ayam online

hoki311

hoki311

KUPU178

KUPU178

KUPU178

KUPU178

KUPU178

ceri188

ceri188

ceri188

togel online

hoki311

hoki311

sabung ayam online

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

KUPU178

KUPU178

KUPU178

hoki311

hoki311

hoki311

hoki311

hoki311

RATU311

KUPU178

THOR311

THOR311

login hoki311

daftar hoki311

hoki 311

link hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

KUPU178

thor311

thor311

thor311

ceri188

ceri188

ceri188

ceri188

link ceri188

KUPU178

KUPU178

KUPU178

kupu178

ceri188

ceri188

ceri188

ceri188

ceri188

THOR311

ceri188

games online ceri188

link login ceri188

ceri188

thor311

RATU311

RATU311

RATU311

sbobet88

sbobet

parlay

mix parlay

judi bola online

thor311

RATU311

RATU311

RATU311

RATU311

THOR311

THOR311

THOR311

THOR311

thor311 RTP

thor311

slot gacor

kupu178

slot mahjong

ceri188

sabung ayam

ratu311

THOR311

THOR311

thor311 aplikasi

ratu311 sabung ayam

slot gacor

RATU311

ceri188

ceri188

ceri188

ceri188

KUPU178

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

ceri188

ceri188

ceri188 alternatif

ceri188

link ceri188

ceri188

RATU311

THOR311

THOR311

THOR311

RATU311

RATU311

RATU311

Togel Online

KUPU178

Situs Togel

KUPU178

ceri188

ceri188

KUPU178

KUPU178

ceri188

ceri188

ratu311

ceri188

ceri188

ratu311

KUPU178

THOR311

ceri188

ceri188

THOR311

THOR311

RATU311

thor311 tajen bali

thor311 alternatif

situs toto4d

thor311 toto4d

thor311 akses

ceri188

daftar ceri188

cery188

ceri188

slot gacor

kupu 178

kupu178

ceri1888

ceri188

slot online

hoki311

togel online

ceri188

ding dong

ratu311

slot gacor

kupu178

live casino

kupu178

mix parlay

ceri188

judi bola online

thor311 slot

THOR311

RATU311

THOR311

ceri188

ratu311

slot online

hoki311

kupu178

togel online

kupu178

slot gacor

ceri188

judi bola online

ceri188

ratu311

kupu178

RATU311

RATU311

live casino roullete

ceri188

slot pg soft

kupu178

KUPU178

THOR311

RATU311

KUPU178

THOR311

THOR311

thor311 domino qq

thor dingdong

ceri188

ceri188

kupu178

kupu178

togel

kupu178

mix parlay

ceri188

slot online

kupu178

THOR311

THOR311

KUPU178

kupu178

hoki311

togel online

kupu178

ceri188

ceri188

www.vrcorporate.in

phbalance.vn

togel hongkong

kupu178

slot gacor

kupu178

kupu178

ceri188

kupu178

KUPU178

RATU311

slot online

kupu178

THOR311

slot mahjong ways

hoki311

slot gacor

ceri188

kupu178

Casino Baccarat Online

kupu178

kupu178

ceri188

toto macau

KUPU178

Slot Gacor

KUPU178

RATU311

Mahjong ways

KUPU178

KUPU178

KUPU178

RATU311

RATU311

Toto Togel

KUPU178

CERI188

Slot Sweet Bonanza 2500

KUPU178

RATU311

KUPU178 X SLOT777

judi bola online

THOR311

KUPU178 SLOT TERPERCAYA

KUPU178 TOGEL

TOTO 4D MACAU

KUPU178

THOR311

HOKI311

KUPU178

Slot Mahjong Ways

KUPU178

CERI188

Slot Online Resmi

KUPU178

RATU311 SLOT ONLINE

KUPU178

KUPU178: Link Akses Resmi

KUPU178: Strategi Menang Di Game Live Casino

KUPU178

KUPU178: Minimal 200 Rupiah

THOR311

THOR311

THOR311

THOR311

THOR311

KUPU178

Slot Thailand

CERI188

CERI188

THOR311 RTP

RATU311

CERI188

KUPU178 TOTO SLOT

CERI188 TOTO Singapore

THOR311

CERI188

RATU311: Alternatif karnpuracollege

Thor311 Terbaru

THOR311 MAHJONG

RATU311: BONUS SABUNG AYAM

CERI188: Masuk Akun Slot Super Cepat

THOR311

HOKI311

HOKI311

RATU311

HOKI311

KUPU178

RATU311

RATU311

THOR311

CERI188

CERI188

RATU311

RATU311

RATU311

Game Thor311

HOKI311

HOKI311

RATU311

HOKI311

HOKI311

RATU311 NONTON BOLA

HOKI311 Link QRIS EWALLET

RATU311

HOKI311 Link Apk Resmi

RATU311 judi bola

KUPU178

HOKI311

RATU311

THOR311

HOKI311

KUPU178

KUPU178

RATU311

RATU311

KUPU178

THOR311

KUPU178

RATU311

THOR311