How to insert Image into Database and Display it using PHP MySQL
Simple PHP program how to insert an image into the database and retrieve the image and display it.
Here first we create a database and after simple PHP code.
Table name:-imaget
image folder name:-photo
Create a database like...
CREATE TABLE IF NOT EXISTS `imaget` (
`id` int(8) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`photo` longblob NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
You may also like
Insert data or image code
if(isset($_POST['upload']))
{
$conn=mysql_connect("localhost","root","","image");//connect database
if(!$conn) //check connection
{
die('connect'.mysql_error());
}
mysql_select_db("image");//select database
$name=$_POST['name'];
$file_name=$_FILES['file']['name'];
$file_type=$_FILES['file']['type'];
$file_size=$_FILES['file']['size'];
$tmp_name=$_FILES['file']['tmp_name'];
mysql_query("insert into imaget(name,photo)values('$name','$file_name')");//insert data into table
move_uploaded_file($tmp_name,"photo/".$file_name);//photo is folder name
//html code
<html> <head><title>simple image upload and display</title></head> </body> <form action='image.php'method='post'enctype='multipart/form-data'> Name:-<input type='text'name='name'><br> <input type='file'name='file'value='upload'><br> <input type='submit'name='upload'value='upload'> </form> </body> </html> //Select query to retrieve data into database
$conn=mysql_connect("localhost","root",""); if(!$conn) { die(mysql_error()); } mysql_select_db("image"); $result=mysql_query("select *from imaget");//select data from table echo"<table border=5px><tr> <th>image/Name</th>"; while($row=mysql_fetch_array($result)) { echo"<tr>"; echo"<td>"; echo"<img src='photo/".$row['photo']."'width='45px'>"; echo $row['photo']; echo"</td>"; echo"</tr>"; } echo"</table>";
//html code
<html> <head><title>simple image upload and display</title></head> </body> <form action='image.php'method='post'enctype='multipart/form-data'> Name:-<input type='text'name='name'><br> <input type='file'name='file'value='upload'><br> <input type='submit'name='upload'value='upload'> </form> </body> </html> //Select query to retrieve data into database
$conn=mysql_connect("localhost","root",""); if(!$conn) { die(mysql_error()); } mysql_select_db("image"); $result=mysql_query("select *from imaget");//select data from table echo"<table border=5px><tr> <th>image/Name</th>"; while($row=mysql_fetch_array($result)) { echo"<tr>"; echo"<td>"; echo"<img src='photo/".$row['photo']."'width='45px'>"; echo $row['photo']; echo"</td>"; echo"</tr>"; } echo"</table>";
Comments
Post a Comment