how to upload image in php and store in database with live demo

How to upload images to PHP and use MySQL used language: HTML, CSS, and PHP for backend. 

Total file: 2 is the first image upload file and the second is to get the image in the database

Created folder: Here, the first is the main folder and the second subfolder is created.

 When we upload, the first folder store 2 PHP files, and the second folder stores the image.

 Here, an attempt is made to explain how to upload images to a database and retrieve it using PHP. 

This is an easy way to upload and display pictures. 

When you upload your image, it will store it in a folder. 

First, we will create a database like contactdb.

 After creating the table. We will create two files, the first file to store the uploaded image and the second PHP file.


Upload image code in PHP

<!-- contact form-->

<?php
$conn=mysqli_connect("localhost","root","");
if(!$conn){
die('could not connect');
}
$db=mysqli_select_db($conn,"contactdb");

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

$fname=$_POST['txtfname'];
$addr=$_POST['txtaddr'];
$email=$_POST['txtemail'];
$message=$_POST['txtmessage'];

$query="insert into contacttab(fullname,address,email,message)values('$fname','$addr','$email','$message')"or die('could not connect');
$result=mysqli_query($conn,$query);

}
?>
<style>
body{align-items:center;display:flex;justify-content:center;}

*{text-aling:center;box-sizing:border-box;}

form{font-family-sants-sarif;}
}

input,select,texarea
{
width:100px;padding:12px;height:30px;margin:10px;
box-sizing:border-box;margin-top:6px;margin-buttom:24px;
border:1px solid red;
}

input[type=submit]
{
background:red;width:210px;
color:white;
font-size:15px;
}

input,select,textarea,button{
font-family:inherit;
font-size:100%;}

.container{border-radius:5px;
background:blue;
padding:40px;color:white;
input[type=reset]{

</style>

<div class="container">
<h1>Contact Form</h1>
<form method="POST"action="contact.php">
<tr>
<td>Full Name</td><span style="color:red">*</span><br>
<td><input type="text"name="txtfname"placeholder="Full Name"required><br></td>
</tr>
<tr><td>
Address</td><span style="color:red">*</span><br>
<td><textarea name="txtaddr"cols="23"rows="2"placeholder="Your address"required></textarea><br>
</td></tr>
<tr><td>
Email</td><span style="color:red">*</span><br>
<td><input type="email"name="txtemail"placeholder="ex.text@gmal.com"required><br>
</td></tr>
<tr><td>
Message</td><span style="color:red">*</span><br>
<td><textarea name="txtmessage"cols="23"rows="2"placeholder="Message"required></textarea><br>
</td></tr><br>
<input type="submit"name="submit"value="submit"><br>
<input type="reset"class="reset"/>

</form>
</div>

                                                       Live Demo


You may also like

Retrieve image 

After creating the uploaded file in PHP Now, we will create a retrieved image in the database file. Here, easily explain how to display images in a database.
 See the code.

<?php
$conn=mysqli_connect("localhost","root","");
$db=mysqli_select_db($conn,"imgdb");

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

$name=$_POST['txtname'];
$file_name=$_FILES['image']['name'];
$tmp_name=$_FILES['image']['tmp_name'];
$file_size=$_FILES['image']['size'];

$query="insert into imgtab(name,image)values('$name','$file_name')"or die('something worong');

$result=mysqli_query($conn,$query);

move_uploaded_file($tmp_name,"img/".$file_name);
}

?>

<form action="img.php"method="POST"enctype="multipart/form-data"/>
<label>Name</label>
<input type="text"name="txtname"><br>
<input type="file"name="image"><br>
<input type="submit"name="upload">
</form>

<!-- fetch image -->

<?php

$conn=mysqli_connect("localhost","root","");
$db=mysqli_select_db($conn,"imgdb");

$query="select *from imgtab";
$result=mysqli_query($conn,$query);

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

echo $row['name'];
echo"<img src='img/".$row['image']."'width='50px'height='50px';>";

}

?>

Live Demo





Please feel free to contact or comment me if you need any further information.

Comments