Today I am talk about. How to form insert,update and delete a simple
html upload image with time stamp. I am just showing step by step all
process. So, let go.............,
AT first create a simple html file. like name as index.html and code will be below.
///////////////////////////////////////////////////////////////////index.html/////////////////////////////////////////////////////
<form action="saveimage.php" enctype="multipart/form-data" method="post">
<table>
<tbody><tr>
<td><input name="uploadedimage" type="file"></td></tr>
<tr><td>
<input name="Upload Now" type="submit" value="Upload Image">
</td></tr>
</tbody></table>
</form>
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Second Create a New Database Name and Table:
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1. Syntax of DB: CREATE DATABASE dbname;( i use db name is: image_db).
2. Syntax of Table: CREATE TABLE images_tbl(images_id int(10) AUTO-INCREMENT,primary,
images_path varchar(255),
submission_date data,
);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Connection to Database with html form process into php and myslq As like as......
/////////////////////////////////////////////////////saveimage.php////////////////////////////////////////////////////////////////////////
<?php
/**********MYSQL Settings****************/
$host="localhost";
$databasename="image_db";
$user="root";
$pass="";
$conn=mysqli_connect($host,$user,$pass,$databasename);
$db_selected = mysqli_select_db($conn,$databasename);
/**************************/
/*Create a Functions*/
function GetImageExtension($imagetype)
{
if(empty($imagetype)) return false;
switch($imagetype)
{
case 'image/bmp': return '.bmp';
case 'image/gif': return '.gif';
case 'image/jpeg': return '.jpg';
case 'image/png': return '.png';
default: return false;
}
}
/*Form Checking*/
if (!empty($_FILES["uploadedimage"]["name"])) {
$file_name=$_FILES["uploadedimage"]["name"];
$temp_name=$_FILES["uploadedimage"]["tmp_name"];
$imgtype=$_FILES["uploadedimage"]["type"];
$ext= GetImageExtension($imgtype);//Adding a function with image extension
$imagename=$_FILES["uploadedimage"]["name"];
$target_path = "images/".$imagename;
/*Validation same image file*/
if(file_exists($target_path)){
echo "File is Already Exsists";
return true;
}
/*Protected image size just edit what you went */
if($_FILES['uploadedimage']['size']>500000000){
echo "File is Very Large, <b>Please check your Images</b>";
return true;
}
/*Move image tmp file to target file*/
if(move_uploaded_file($temp_name, $target_path)) {
$query_upload="INSERT INTO images_tbl (images_path,submission_date)VALUES
('".$target_path."','".date("Y-m-d")."')";
$result=mysqli_query($conn,$query_upload) or die("error in $query_upload == ".mysql_error());
$id = (int) mysql_insert_id($result);
header('Location:imag_view.php');
}else{
exit("Error While uploading image on the server");
}
}
Note: If you have to get some error. Please check you php and mysql version and your code will be run successfully.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
when Insert your image at your Database Please check to show image the code will be below:
/////////////////////////////////////////////imag_view.php/////////////////////////////////////////////////////////////////////////////
<?php
/**********MYSQL Settings*********************************************/
$host="localhost";
$databasename="image_db";
$user="root";
$pass="";
$conn=mysqli_connect($host,$user,$pass,$databasename);
$db_selected = mysqli_select_db($conn,$databasename);
/*******************************************************************/
/*mysql select query adding here*/
$sql=mysqli_query($conn,"SELECT * FROM images_tbl");
echo "<table border='1'>";//Just create a simple table to show all data here;
echo "<tr>
<th>ID</th>
<th>Images</th>
<th>Edit</th>
<th>Delete</th>
</tr>";
while($row = mysqli_fetch_array($sql,MYSQL_BOTH)){// MYSQL FETCH ARRAY LOOP
$id=$row['images_id'];
?>
<tr><td><?php echo $row['images_id']; ?></td>
<td><?php echo "<img src='".$row['images_path']."'>"; ?></td>
<td><a href="edit.php?images_id=<?php echo $id; ?>">Edit</a></td>
<td><a href="delete.php?images_id=<?php echo $id?>">Delete</a></td>
</tr>
<?php
}
echo "</table>";
?>
//Simple style adding here if your image show large
<style type="text/css">
img{
width: 120px;
}
</style>
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
If you wish to edit your image again show your table code will be like here:
///////////////////////////////////////////////edit.php//////////////////////////////////////////////////////////////////////////////////////
<?php
/**********MYSQL Settings****************/
$host="localhost";
$databasename="image_db";
$user="root";
$pass="";
$conn=mysqli_connect($host,$user,$pass,$databasename);
$db_selected = mysqli_select_db($conn,$databasename);
/**************************/
/*Create function for image extension*/
function GetImageExtension($imagetype)
{
if(empty($imagetype)) return false;
switch($imagetype){
case 'image/bmp': return '.bmp';
case 'image/gif': return '.gif';
case 'image/jpeg': return '.jpg';
case 'image/png': return '.png';
default: return false;
}
}
/*Checking a image conditions*/
if (!empty($_FILES["uploadedimage"]["name"])) {
$file_name=$_FILES["uploadedimage"]["name"];
$temp_name=$_FILES["uploadedimage"]["tmp_name"];
$imgtype=$_FILES["uploadedimage"]["type"];
$ext= GetImageExtension($imgtype);
$imagename=$_FILES["uploadedimage"]["name"];
$target_path = "images/".$imagename;
/*Not allowed a same image again uploaded*/
if(file_exists($target_path)){
echo "File is Already Exsists";
return true;
}
/*Check Image size use large size if you use less, just your choices */
if($_FILES['uploadedimage']['size']>500000000){
echo "File is Very Large, <b>Please check your Images</b>";
return true;
}
/*move image to target files which is name is images */
if(move_uploaded_file($temp_name, $target_path)) {
$id=$_GET['images_id'];
$sql=mysqli_query($conn,"UPDATE images_tbl SET images_path='$target_path', submission_date='".date("Y-m-d")."' WHERE images_id='$id'");//image updat query
$sql1=mysqli_query($conn,"SELECT * FROM images_tbl");//select query again
while($row=mysqli_fetch_array($sql1,MYSQL_BOTH)){//fetch array
?>
echo $row['images_id'];
echo "<img src='".$row['images_path']."'>";
}
header('Location:imag_view.php');
}else{
exit("Error While uploading image on the server");
}
}
?>
<!---Again insert form Here, because if you change your image than it's need it. --->
<form action="" enctype="multipart/form-data" method="post">
<table style="border-collapse: collapse; font: 12px Tahoma;" border="1" cellspacing="5" cellpadding="5">
<tbody><tr>
<td>
<input name="uploadedimage" type="file">
</td>
</tr>
<tr>
<td>
<input name="Update" type="submit" value="Update Image">
</td>
</tr>
</tbody></table>
</form>
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
And Finally Adding Delete files,,,,,,,,,,,,,,,
///////////////////////////////////////delete.php/////////////////////////////////////////////////////////////////////////////////////////
<?php
/**********MYSQL Settings****************/
$host="localhost";
$databasename="image_db";
$user="root";
$pass="";
$conn=mysqli_connect($host,$user,$pass,$databasename);
$db_selected = mysqli_select_db($conn,$databasename);
/**************************/
/*setup delete query*/
$id=$_GET['images_id'];
$del=mysqli_query($conn,"DELETE FROM images_tbl WHERE images_id='$id'");
if($del){
echo "Data is Successfully Deleted";
header('Location:imag_view.php');
}else{
echo 'Does not delete this items';
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
I use simple form for image upload coding, If you find any kinds of error please knocked me. I am always in online. Thank you very much.
AT first create a simple html file. like name as index.html and code will be below.
///////////////////////////////////////////////////////////////////index.html/////////////////////////////////////////////////////
<form action="saveimage.php" enctype="multipart/form-data" method="post">
<table>
<tbody><tr>
<td><input name="uploadedimage" type="file"></td></tr>
<tr><td>
<input name="Upload Now" type="submit" value="Upload Image">
</td></tr>
</tbody></table>
</form>
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Second Create a New Database Name and Table:
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1. Syntax of DB: CREATE DATABASE dbname;( i use db name is: image_db).
2. Syntax of Table: CREATE TABLE images_tbl(images_id int(10) AUTO-INCREMENT,primary,
images_path varchar(255),
submission_date data,
);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Connection to Database with html form process into php and myslq As like as......
/////////////////////////////////////////////////////saveimage.php////////////////////////////////////////////////////////////////////////
<?php
/**********MYSQL Settings****************/
$host="localhost";
$databasename="image_db";
$user="root";
$pass="";
$conn=mysqli_connect($host,$user,$pass,$databasename);
$db_selected = mysqli_select_db($conn,$databasename);
/**************************/
/*Create a Functions*/
function GetImageExtension($imagetype)
{
if(empty($imagetype)) return false;
switch($imagetype)
{
case 'image/bmp': return '.bmp';
case 'image/gif': return '.gif';
case 'image/jpeg': return '.jpg';
case 'image/png': return '.png';
default: return false;
}
}
/*Form Checking*/
if (!empty($_FILES["uploadedimage"]["name"])) {
$file_name=$_FILES["uploadedimage"]["name"];
$temp_name=$_FILES["uploadedimage"]["tmp_name"];
$imgtype=$_FILES["uploadedimage"]["type"];
$ext= GetImageExtension($imgtype);//Adding a function with image extension
$imagename=$_FILES["uploadedimage"]["name"];
$target_path = "images/".$imagename;
/*Validation same image file*/
if(file_exists($target_path)){
echo "File is Already Exsists";
return true;
}
/*Protected image size just edit what you went */
if($_FILES['uploadedimage']['size']>500000000){
echo "File is Very Large, <b>Please check your Images</b>";
return true;
}
/*Move image tmp file to target file*/
if(move_uploaded_file($temp_name, $target_path)) {
$query_upload="INSERT INTO images_tbl (images_path,submission_date)VALUES
('".$target_path."','".date("Y-m-d")."')";
$result=mysqli_query($conn,$query_upload) or die("error in $query_upload == ".mysql_error());
$id = (int) mysql_insert_id($result);
header('Location:imag_view.php');
}else{
exit("Error While uploading image on the server");
}
}
Note: If you have to get some error. Please check you php and mysql version and your code will be run successfully.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
when Insert your image at your Database Please check to show image the code will be below:
/////////////////////////////////////////////imag_view.php/////////////////////////////////////////////////////////////////////////////
<?php
/**********MYSQL Settings*********************************************/
$host="localhost";
$databasename="image_db";
$user="root";
$pass="";
$conn=mysqli_connect($host,$user,$pass,$databasename);
$db_selected = mysqli_select_db($conn,$databasename);
/*******************************************************************/
/*mysql select query adding here*/
$sql=mysqli_query($conn,"SELECT * FROM images_tbl");
echo "<table border='1'>";//Just create a simple table to show all data here;
echo "<tr>
<th>ID</th>
<th>Images</th>
<th>Edit</th>
<th>Delete</th>
</tr>";
while($row = mysqli_fetch_array($sql,MYSQL_BOTH)){// MYSQL FETCH ARRAY LOOP
$id=$row['images_id'];
?>
<tr><td><?php echo $row['images_id']; ?></td>
<td><?php echo "<img src='".$row['images_path']."'>"; ?></td>
<td><a href="edit.php?images_id=<?php echo $id; ?>">Edit</a></td>
<td><a href="delete.php?images_id=<?php echo $id?>">Delete</a></td>
</tr>
<?php
}
echo "</table>";
?>
//Simple style adding here if your image show large
<style type="text/css">
img{
width: 120px;
}
</style>
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
If you wish to edit your image again show your table code will be like here:
///////////////////////////////////////////////edit.php//////////////////////////////////////////////////////////////////////////////////////
<?php
/**********MYSQL Settings****************/
$host="localhost";
$databasename="image_db";
$user="root";
$pass="";
$conn=mysqli_connect($host,$user,$pass,$databasename);
$db_selected = mysqli_select_db($conn,$databasename);
/**************************/
/*Create function for image extension*/
function GetImageExtension($imagetype)
{
if(empty($imagetype)) return false;
switch($imagetype){
case 'image/bmp': return '.bmp';
case 'image/gif': return '.gif';
case 'image/jpeg': return '.jpg';
case 'image/png': return '.png';
default: return false;
}
}
/*Checking a image conditions*/
if (!empty($_FILES["uploadedimage"]["name"])) {
$file_name=$_FILES["uploadedimage"]["name"];
$temp_name=$_FILES["uploadedimage"]["tmp_name"];
$imgtype=$_FILES["uploadedimage"]["type"];
$ext= GetImageExtension($imgtype);
$imagename=$_FILES["uploadedimage"]["name"];
$target_path = "images/".$imagename;
/*Not allowed a same image again uploaded*/
if(file_exists($target_path)){
echo "File is Already Exsists";
return true;
}
/*Check Image size use large size if you use less, just your choices */
if($_FILES['uploadedimage']['size']>500000000){
echo "File is Very Large, <b>Please check your Images</b>";
return true;
}
/*move image to target files which is name is images */
if(move_uploaded_file($temp_name, $target_path)) {
$id=$_GET['images_id'];
$sql=mysqli_query($conn,"UPDATE images_tbl SET images_path='$target_path', submission_date='".date("Y-m-d")."' WHERE images_id='$id'");//image updat query
$sql1=mysqli_query($conn,"SELECT * FROM images_tbl");//select query again
while($row=mysqli_fetch_array($sql1,MYSQL_BOTH)){//fetch array
?>
echo $row['images_id'];
echo "<img src='".$row['images_path']."'>";
}
header('Location:imag_view.php');
}else{
exit("Error While uploading image on the server");
}
}
?>
<!---Again insert form Here, because if you change your image than it's need it. --->
<form action="" enctype="multipart/form-data" method="post">
<table style="border-collapse: collapse; font: 12px Tahoma;" border="1" cellspacing="5" cellpadding="5">
<tbody><tr>
<td>
<input name="uploadedimage" type="file">
</td>
</tr>
<tr>
<td>
<input name="Update" type="submit" value="Update Image">
</td>
</tr>
</tbody></table>
</form>
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
And Finally Adding Delete files,,,,,,,,,,,,,,,
///////////////////////////////////////delete.php/////////////////////////////////////////////////////////////////////////////////////////
<?php
/**********MYSQL Settings****************/
$host="localhost";
$databasename="image_db";
$user="root";
$pass="";
$conn=mysqli_connect($host,$user,$pass,$databasename);
$db_selected = mysqli_select_db($conn,$databasename);
/**************************/
/*setup delete query*/
$id=$_GET['images_id'];
$del=mysqli_query($conn,"DELETE FROM images_tbl WHERE images_id='$id'");
if($del){
echo "Data is Successfully Deleted";
header('Location:imag_view.php');
}else{
echo 'Does not delete this items';
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
I use simple form for image upload coding, If you find any kinds of error please knocked me. I am always in online. Thank you very much.