Make Delete Multiple Rows v2 PHP Script
Make Delete Multiple Rows v2 PHP Script
In this tutorial, we are going to learn on How To Make Delete Multiple Rows Using PHP. The features of this project is to add a data then, delete multiple data using check box as our selector. Hope you find this useful.
In this tutorial, we are going to learn on How To Make Delete Multiple Rows Using PHP. The features of this project is to add a data then, delete multiple data using check box as our selector. Hope you find this useful.
For Javascript - Select All Rows
<script language="JavaScript">
function sel(source)
{
checkboxes = document.getElementsByName('check[]');
for(var i in checkboxes)
checkboxes[i].checked = source.checked;
}
</script>
For PHP - Add Data
if(isset($_POST['submit']))
{
$userid=$_POST['userid'];
$name=$_POST['name'];
$username=$_POST['username'];
$email=$_POST['email'];
$address=$_POST['address'];
$id1 = $_SERVER['REMOTE_ADDR'];
$insert=mysql_query("insert into user (userid,name,username,email,address) values ('$userid','$name','$username','$email','$address')");
if($insert)
{
echo "<script>alert('User has been added'); window.location='index.php'</script>";
}
}
For PHP - Delete Multiple Rows of Data
if(isset($_POST['delete']))
{
$check=$_POST['check'];
$count=count($check);
for($i=0;$i<$count;$i++){
$del_id = $check[$i];
$delete=mysql_query("delete from user where id='$del_id'") or die(mysql_error());
}
if($delete)
{
echo "<script>alert('User has been deleted'); window.location='index.php'</script>";
}
}
Full Source Code
<!DOCTYPE html>
<html>
<head>
<title>Delete Multiple Rows</title>
<script language="JavaScript">
function sel(source)
{
checkboxes = document.getElementsByName('check[]');
for(var i in checkboxes)
checkboxes[i].checked = source.checked;
}
</script>
<style type="text/css">
body {
width:100%;
font-family:"Trebuchet MS";
margin:0;
padding:0;
}
h2 a {
text-decoration:none;
color:#06F;
}
#container {
width:800px;
margin:0 auto;
margin-top:30px;
}
</style>
</head>
<body>
<?php
$db = mysql_connect('localhost','root','') or die ("Unable to connect to Database Server.");
mysql_select_db ('demo', $db) or die ("Could not select database.");
if(isset($_POST['submit']))
{
$userid=$_POST['userid'];
$name=$_POST['name'];
$username=$_POST['username'];
$email=$_POST['email'];
$address=$_POST['address'];
$id1 = $_SERVER['REMOTE_ADDR'];
$insert=mysql_query("insert into user (userid,name,username,email,address) values ('$userid','$name','$username','$email','$address')");
if($insert)
{
echo "<script>alert('User has been added'); window.location='index.php'</script>";
}
}
if(isset($_POST['delete']))
{
$check=$_POST['check'];
$count=count($check);
for($i=0;$i<$count;$i++){
$del_id = $check[$i];
$delete=mysql_query("delete from user where id='$del_id'") or die(mysql_error());
}
if($delete)
{
echo "<script>alert('User has been deleted'); window.location='index.php'</script>";
}
}
?>
<form method="post" name="form" action="">
<table border="1" style="border:4px groove #CCC; float:left; margin-left:80px;" cellpadding="4" cellspacing="4">
<tbody>
<tr>
<td colspan="2" style="text-align:center; font-weight:bold;">Add User</td>
</tr>
<tr>
<td>
<span style="color:blue;">User ID</span>
</td>
<td>
<input name="userid" style="font-size:15px;" placeholder="User ID..." type="text" id="userid" required />
</td>
</tr>
<tr>
<td>
<span style="color:blue;">Name</span>
</td>
<td>
<input name="name" style="font-size:15px;" placeholder="Name..." type="text" id="name" required />
</td>
</tr>
<tr>
<td>
<span style="color:blue;">Username</span>
</td>
<td>
<input name="username" type="text" style="font-size:15px;" placeholder="Username..." id="username" required />
</td>
</tr>
<tr>
<td>
<span style="color:blue;">Email</span>
</td>
<td>
<input name="email" type="email" style="font-size:15px;" placeholder="Email..." id="email" required />
</td>
</tr>
<tr>
<td>
<span style="color:blue;">Address</span>
</td>
<td>
<input name="address" style="font-size:15px;" placeholder="Address..." type="text" id="address" required />
</td>
</tr>
<tr>
<td align="center" colspan="2">
<input type="submit" style="color:#fff; background-color:#CCC; border:2px groove chocolate; border-radius:4px; font-size:15px;" name="submit" value="Add Employee" id="submit" />
</td>
</tr>
</tbody>
</table>
</form>
<!---Viewing-->
<form method="post">
<table border="1" style="border:4px groove #CCC; float:right; margin-right:80px;" cellpadding="0" cellspacing="0" id="container">
<tr align="center">
<td>
<input type="checkbox" onClick="return sel(this);"/> <span>Select All</span>
</td>
<td>
<strong>User ID</strong>
</td>
<td>
<strong>Name</strong>
</td>
<td>
<strong>Username</strong>
</td>
<td>
<strong>Email</strong>
</td>
<td>
<strong>Address</strong>
</td>
</tr>
<?php
$id1 = $_SERVER['REMOTE_ADDR'];
$result=mysql_query("select * from user order by id ASC ") or die(mysql_error());
while($user_info=mysql_fetch_array($result)){
?>
<tr align="center">
<td>
<input type="checkbox" name="check[]" value="<?php echo $user_info['id']; ?>" id="all" />
</td>
<td>
<?php echo $user_info['userid']; ?>
</td>
<td>
<?php echo $user_info['name']; ?>
</td>
<td>
<?php echo $user_info['username']; ?>
</td>
<td>
<?php echo $user_info['email']; ?>
</td>
<td>
<?php echo $user_info['address']; ?>
</td>
</tr>
<?php } ?>
<tr>
<td colspan="6">
<p align="center">
<input type="submit" style="color:#fff; background-color:#CCC; border:2px groove chocolate; border-radius:4px; font-size:15px;" name="delete" value="Delete" />
</p>
</td>
</tr>
</table>
</form>
</body>
</html>
Comments
Post a Comment