CRUD with image upload is a very common task in web development (CRUD stands for Create/Read/Update/Delete). The main purpose of a CRUD is that enables user’s create/read/update/delete data using PHP & MYSQL.
Table of Content
1. Create Database
2. Database Connection
3. Show Record
4. Add Record
5. Edit Record
6. Delete Record
1.Create Database
CREATE TABLE IF NOT EXISTS `tbl_emp` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`image` varchar(255) NOT NULL,
`status` int(1) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
2. Database Connectionerror_reporting(0);
$server = 'localhost';
$user = 'root';
$pass = '';
$connect = mysql_connect($server,$user,$pass)
or die(mysql_error());
$selectdb = mysql_select_db('crud')
or die(mysql_error());
3. Show Record<a href="add.php" class="btn btn-xs btn-primary pull-left">Add New Record</a>
<form method="post" name="action_form" action="index.php">
<table class="table table-striped" >
<thead>
<tr>
<th>Select</th>
<th>Name</th>
<th>Image</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$allRecords = mysql_query('select * from tbl_emp ORDER BY id DESC limit 5');
if(is_resource($allRecords))
{
while($row = mysql_fetch_assoc($allRecords))
{
?>
<tr>
<td align="left"><input type="checkbox" value="<?php echo $row['id'];?>" name="ids[]"></td>
<td align="left"><?php echo $row['name']; ?></td>
<td align="left">
<?php
if(!empty($row['image']))
{
?>
<img src="uploads/<?php echo $row['image']; ?>" height="30" width="30" />
<?php
}
?>
</td>
<td align="left"><a href="add.php?id=<?php echo $row['id']; ?>">Edit</a></td>
</tr>
<?php
}
}
?>
<tr>
<td colspan="4" align="left">
<select name="action" class="form-control-sm" style="width:150px;float:left;">
<option>Choose an action</option>
<option value="delete">Delete</option>
</select>
<input class="btn btn-xs btn-primary" type="submit" name="Go" class="submit" value="Apply action">
</td>
</tr>
</tbody>
</table>
</form>
4. Add & Edit Record<?php
if(isset($_GET['id']))
{
$qry = "SELECT * FROM tbl_emp where id=".$_GET['id'];
$result = mysql_query($qry);
$row = mysql_fetch_array($result);
if($_SERVER["REQUEST_METHOD"] == "POST")
{
$name = $_POST["name"];
IF($_FILES['file']['name']!='')
{
$file='uploads/'.$row['image'];
@unlink($file);
$tmp_name = $_FILES["file"]["tmp_name"];
$namefile = $_FILES["file"]["name"];
$ext = end(explode(".", $namefile));
$image_name=time().".".$ext;
$fileUpload = move_uploaded_file($tmp_name,"uploads/".$image_name);
}
else
{
$image_name=$row['image'];
}
$sqlAdd ="update tbl_emp set name='".$name."', image='".$image_name."' where id=".$_GET['id'];
mysql_query($sqlAdd);
header("Location:add.php?id=".@$_GET['id']."&msg=success");
exit;
}
}
else
{
if($_SERVER["REQUEST_METHOD"] == "POST")
{
$name = $_POST["name"];
IF($_FILES['file']['name']!='')
{
$tmp_name = $_FILES["file"]["tmp_name"];
$namefile = $_FILES["file"]["name"];
$ext = end(explode(".", $namefile));
$image_name=time().".".$ext;
$fileUpload = move_uploaded_file($tmp_name,"uploads/".$image_name);
}
$sqlAdd = mysql_query("insert into tbl_emp(name,image) VALUES('$name','$image_name')");
header("Location:index.php?msg=success");
exit;
}
}
ob_end_flush();
if(isset($_GET['msg']))
{
?>
<div class="form-message alert alert-success" align="center"><b>Task completd successfully.</b></div>
<?php
}
?>
<form method="post" name="login" id="login" enctype="multipart/form-data">
<table class="table table-striped" >
<tr>
<td width="10%">Name</td>
<td><input name="name" type="text" placeholder="Enter Name" required value="<?php echo @$row['name'];?>" class="form-control"></td>
</tr>
<tr>
<td width="10%">Image</td>
<td><input name="file" type="file" class="form-control"></td>
</tr>
<?php
if(isset($row['image']))
{
?>
<tr>
<td> </td>
<td align="left"><img src="uploads/<?php echo $row['image'];?> " height="50" width="50"></td>
</tr>
<?php
}
?>
<tr>
<td> </td>
<td align="left">
<input name="submit" value="Submit" type="submit" class="btn btn-xs btn-primary">
<input name="submit" value="Cancel" type="button" class="btn btn-xs btn-primary" onClick="window.location='index.php'">
</td>
</tr>
</table>
</form>
5. Delete Record<?php
$ids="";
if($_SERVER["REQUEST_METHOD"] == "POST")
{
if($_POST['action'] == "delete")
{
$ids = @implode(", ", $_POST['ids']);
$qry = "SELECT * FROM tbl_emp where id IN(".$ids.")";
$result = mysql_query($qry);
while($row = mysql_fetch_array($result))
{
$file='uploads/'.$row['image'];
@unlink($file);
}
$sqlAdd ="delete from tbl_emp where id IN(".$ids.")";
mysql_query($sqlAdd);
header("Location:index.php?msg=success");
exit;
}
}
ob_end_flush();
if(isset($_GET['msg']))
{
?>
<div class="form-message alert alert-success" align="center"><b>Task completd successfully.</b></div>
<?php
}
?>
DOWNLOAD CODE
Here you can download source code with db file for above demo its very simple you can change as your requirement.
Thanks,
Download Code
Live Demo
Post a Comment