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

judi bola online

ceri188

ratu311

kupu178

RATU311

RATU311

live casino roullete

ceri188

slot pg soft

kupu178

ceri188

HOKI311

KUPU178

RATU311

RATU311

RATU311

KUPU178

KUPU178

THOR311

KUPU178

RATU311

THOR311

THOR311

THOR311

THOR311

RATU311

RATU311

KUPU178

KUPU178

KUPU178

THOR311

THOR311

THOR311

THOR311

RATU311

RATU311

RATU311

RATU311

thor311 domino qq

thor311 akses

thor dingdong

THOR311

ceri188

ceri188

HOKI311

KUPU178

RATU311

RATU311

kupu178

kupu178

HOKI311

togel

kupu178

mix parlay

ceri188

slot online

kupu178

THOR311

THOR311

KUPU178

KUPU178

KUPU178

RATU311

kupu178

hoki311

THOR311

togel online

kupu178

ceri188

ceri188

mimcord.com

Centrum Medyczne

www.vrcorporate.in

phbalance.vn

KUPU178

togel hongkong

kupu178

slot gacor

kupu178

kupu178

ceri188

kupu178

ceri188

KUPU178

KUPU178

kupu178

RATU311

RATU311

RATU311

KUPU178

KUPU178

RATU311

RATU311

slot online

kupu178

thor311

thor311

kupu178

kupu178

THOR311

THOR311

KUPU178

slot mahjong ways

hoki311

slot gacor

ceri188

kupu178

Casino Baccarat Online

kupu178

kupu178

ceri188

ceri188

THOR311

RATU311

toto macau

KUPU178

Slot Gacor

KUPU178

RATU311

Slot Gacor Online

THOR311

Mahjong ways

KUPU178

THOR311

THOR311

CERI188

CERI188

THOR311

THOR311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

KUPU178

KUPU178

CERI188

CERI188 SITE

CERI188

RATU311

RATU311

RATU311

Toto Togel

KUPU178

CERI188

CERI188

Slot Sweet Bonanza 2500

KUPU178

RATU311

RATU311

RATU311

KUPU178 X SLOT777

judi bola online

THOR311

KUPU178 SLOT TERPERCAYA

KUPU178 TOGEL

TOTO 4D MACAU

KUPU178

KUPU178

RATU311

THOR311

HOKI311

KUPU178

RATU311

THOR311

CERI188

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

Slot Gacor Online

KUPU178

Mix Parlay

CERI188

CERI188

THOR311

THOR311

THOR311

THOR311

THOR311

KUPU178

Slot Thailand

CERI188

CERI188

THOR311 RTP

RATU311

RATU311

RATU311

CERI188

KUPU178 TOTO SLOT

CERI188 TOTO Singapore

THOR311

KUPU178

KUPU178

RATU311

CERI188

RATU311: Alternatif karnpuracollege

RATU311

RATU311

Thor311 Terbaru

THOR311 MAHJONG

RATU311: BONUS SABUNG AYAM

CERI188: Masuk Akun Slot Super Cepat

CERI188

CERI188

CERI188

THOR311

HOKI311

HOKI311

RATU311

HOKI311

KUPU178

RATU311

RATU311

RATU311

RATU311

THOR311

CERI188

CERI188

RATU311

RATU311

RATU311

RATU311

Game Thor311

HOKI311

HOKI311

RATU311

HOKI311

HOKI311

THOR311

RATU311 NONTON BOLA

HOKI311 Link QRIS EWALLET

RATU311

HOKI311 Link Apk Resmi

CERI188

RATU311 judi bola

KUPU178

situs judi bola

HOKI311

RATU311

THOR311

HOKI311

KUPU178

kupu178 link alternatif

kupu178 slot

kupu178 daftar

kupu178 login

link daftar kupu178

judi bola

RATU311

link alternatif RATU311

Link daftar RATU311

link login RATU311

RATU311

KUPU178

KUPU178

RATU311

RATU311

KUPU178

THOR311

slot thailand

judi bola

KUPU178

slot gacor

slot gacor

slot gacor

RATU311

THOR311