Delete Multiple rows

Delete Multiple rows in php first count the values sy : count(); count to select values in table example 123456 rows .next for loop using to divided a values to delete a table rows.





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>

PHP : Delete Multiple Rows


<?php
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 emp where id='$del_id'") or die(mysql_error());
}
?>

Full Code


<?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']))
{
$id_no=$_POST['id_no'];
$emp_name=$_POST['emp_name'];
$des=$_POST['des'];
$email=$_POST['email'];
$sal=$_POST['sal'];
$insert=mysql_query("insert into emp(id_no,emp_name,des,email,sal) values ('$id_no','$emp_name','$des','$email','$sal')");
if($insert){ $msg1="Successfully Addes!!";}

}
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 emp where id='$del_id'") or die(mysql_error());
}
if($delete){ $msg2="Successfully Deleted!!";}

}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<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;}
#warp{width:800px;margin:0 auto;margin-top:30px;}


</style>
</head>

<body>
<h2><a href="http://crackerworld.blogspot.in">Cracker World</a></h2>
<div style="border-bottom:1px solid #CCC"></div>
<h3 align="center">Add Employee Details</h3>
<form method="post" name="form" action="">
<p align="center" style="color:#390"><?php echo $msg1;?></p>
<p align="center">ID No&nbsp; <input name="id_no" type="text" id="id_no"></p>
<p align="center">Name&nbsp; <input name="emp_name" type="text" id="emp_name" /></p>
<p align="center">Des&nbsp;&nbsp;&nbsp;&nbsp; <input name="des" type="text" id="des" /></p>
<p align="center">Email&nbsp; <input name="email" type="text" id="email" /></p>
<p align="center">Salary&nbsp; <input name="sal" type="text" id="sal" /></p>
<p align="center"><input type="submit" name="submit" value="submit" id="submit" /></p>
</form>
<form method="post">
<p align="center" style="color:#390"><?php echo $msg2;?></p>
<table border="1" cellpadding="0" cellspacing="0" id="warp">
<tr align="center">
<td width="21" align="left"><input type="checkbox" onClick="return sel(this);"/></td>
<td width="296"><strong>Emp ID</strong></td>
<td width="351">
<strong>Emp Name</strong>
</td>
<td width="122">
<strong>Des</strong>
</td>
<td width="122">
<strong>Email</strong>
</td>
<td width="122">
<strong>Salary</strong>
</td>
</tr>

<?php

$sel_emp_tbl=mysql_query("select * from emp") or die(mysql_error());
while($sel_emp_rows=mysql_fetch_array($sel_emp_tbl)){
?>

<tr>
<td><input type="checkbox" name="check[]" value="<?php echo $sel_emp_rows['id']; ?>" id="all" /></td>
<td><?php echo $sel_emp_rows['id_no']; ?></td>
<td><?php echo $sel_emp_rows['emp_name']; ?></td>
<td><?php echo $sel_emp_rows['des']; ?></td>
<td><?php echo $sel_emp_rows['email']; ?></td>
<td><?php echo $sel_emp_rows['sal']; ?></td>
</tr>
<?php } ?>

</table>
<p align="center"><input type="submit" name="delete" value="Delete" /></p>
</form>
</body>
</html>

No comments:

Post a Comment