how to upload image in folder and database in php


 Create table and connected from database 

<?php

$con = mysql_connect (“localhost”,”root”,””);
if (!$con)
{
die (‘Could not connect: ‘ . mysql_error());
}
mysql_select_db (“ratnesh”, $con);
$sql = “CREATE TABLE tbl_image
(
`id` INT( 50 ) NOT NULL AUTO_INCREMENT ,
`photo` VARCHAR( 50 ) NOT NULL ,
`status` INT( 50 ) NOT NULL ,
PRIMARY KEY ( `id` )
)”;
mysql_query($sql,$con);
mysql_close($con);
?>

 Upload  images in database and folder 

<?php

//Working on uplod image in database with folder

$con=mysql_connect( “localhost”,”root”,””);
mysql_select_db (“ratnesh”,$con);
if(isset($_POST [‘submit’]))

{

$file = $_FILES [‘file’];
$name = $file [‘name’];
$type = $file [‘type’];
$size = $file [‘size’];
$stored = $file [‘tmp_name’];

if(file_exists(“storimg/”.$_FILES[“file”][“name”]))
{
echo $_FILES[“file”][“name”] . “alredy exists. “;
}
else

//if($name!=””)
{
if(move_uploaded_file ($stored,’storimg/’.$name))//image is a folder in which you will save image
{
$query=”insert into tbl_image set photo=’$name'”;
mysql_query ($query) or die (‘could not updated:’.mysql_error());
echo “Your image upload successfully !!”;
}
}
}

?>

<html >
<head>
</head>
<body>
<form name=”form”  method=”post” enctype=”multipart/form-data”>
Photo <input type=”file” name=”file” />
<input type=”submit” name=”submit” value=”submit” />
</form>
</body>
</html>

Select images in tabla from database 

<?php
$con=mysql_connect (“localhost”,”root”,””);
mysql_select_db(“ratnesh”,$con);
$sql=”select * from tbl_image where id=’1′ and status=’0′”;
$query=mysql_query($sql);
while($row=mysql_fetch_array($query))
{
$image=$row [‘photo’];
?>
<img src=”img/<?php echo $image; ?>” width=”360″ height=”150″>
<?php
}
?>

Display images from database (display images with data in table )

<?php

$con=mysql_connect(“localhost”,”root”,””);
if(!$con)
{
die(‘could not connect:’ .mysql_error());
}
mysql_select_db(“ratnesh”,$con);

$result=mysql_query(“Select * from data “); ?>
<table border=’1′ width=’50%’ >
<tr>
<th>FirstName</th>
<th> LastName</th>
<th> Email</th>
<th> Password</th>
<th> Cpassword</th>
<th> Education</th>
<th>Phonumber</th>
<th>image</th>
</tr>
<?php
while ($row = mysql_fetch_array($result))
{
echo “<tr>”;
echo “<td>”.$row[‘FirstName’].”</td>”;
echo “<td>”.$row[‘LastName’].”</td>”;
echo “<td>”.$row[‘Email’].”</td>”;
echo “<td>”.$row[‘Password’].”</td>”;
echo “<td>”.$row[‘Cpassword’].”</td>”;
echo “<td>”.$row[‘Education’].”</td>”;
echo “<td>”.$row[‘Phonumber’].”</td>”;
echo “<td>”.$row[‘image’].”</td>”;
//display image and data in use  cmand show i tble
echo “<td><img width=’50px’ height=’50px’ src=’storimg/”.$row[‘image’].”‘></td>”;
//echo “<td>”.$row[‘image’].”width=’5px’ height=’5px'</td>”;
echo “</tr>”;

}
echo “</table>”;

mysql_close($con);

?>

Leave a comment