web 2.0

How to use PHP & MySQL Delete Multiple Rows Record Using Checkbox

How to use PHP & MySQL Delete Multiple Rows Record Using Checkbox Learn PHP how to using PHP delete multiple record from mysql database.

ShotDev Focus:
- PHP & MySQL delete multiple record using checkbox.

Example 1

php_mysql_checkbox1.php


<html>
<head>
<title>ShotDev.Com Tutorial</title>
</head>
<body>
<script language="JavaScript">
function onDelete()
{
if(confirm('Do you want to delete ?')==true)
{
return true;
}
else
{
return false;
}
}
</script>
<form name="frmMain" action="php_mysql_checkbox2.php" method="post" OnSubmit="return onDelete();">
<?
$objConnect = mysql_connect("localhost","root","root") or die(mysql_error());
$objDB = mysql_select_db("mydatabase");
$strSQL = "SELECT * FROM customer";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
?>
<table width="600" border="1">
<tr>
<th width="91"> <div align="center">CustomerID </div></th>
<th width="98"> <div align="center">Name </div></th>
<th width="198"> <div align="center">Email </div></th>
<th width="97"> <div align="center">CountryCode </div></th>
<th width="59"> <div align="center">Budget </div></th>
<th width="71"> <div align="center">Used </div></th>
<th width="30"> <div align="center">Delete </div></th>
</tr>
<?
while($objResult = mysql_fetch_array($objQuery))
{
?>
<tr>
<td><div align="center"><?=$objResult["CustomerID"];?></div></td>
<td><?=$objResult["Name"];?></td>
<td><?=$objResult["Email"];?></td>
<td><div align="center"><?=$objResult["CountryCode"];?></div></td>
<td align="right"><?=$objResult["Budget"];?></td>
<td align="right"><?=$objResult["Used"];?></td>
<td align="center"><input type="checkbox" name="chkDel[]" value="<?=$objResult["CustomerID"];?>"></td>
</tr>
<?
}
?>
</table>
<?
mysql_close($objConnect);
?>
<input type="submit" name="btnDelete" value="Delete">
</form>
</body>
</html>

php_mysql_checkbox2.php


<html>
<head>
<title>ShotDev.Com Tutorial</title>
</head>
<body>
<?
$objConnect = mysql_connect("localhost","root","root") or die(mysql_error());
$objDB = mysql_select_db("mydatabase");

for($i=0;$i<count($_POST["chkDel"]);$i++)
{
if($_POST["chkDel"][$i] != "")
{
$strSQL = "DELETE FROM customer ";
$strSQL .="WHERE CustomerID = '".$_POST["chkDel"][$i]."' ";
$objQuery = mysql_query($strSQL);
}
}

echo "Record Deleted.";

mysql_close($objConnect);
?>
</body>
</html>

Create a php file and save to path root-path/myphp/

Run
http://localhost/myphp/php_mysql_checkbox1.php

Screenshot

PHP MySQL Delete Multiple Record Using Checkbox

.
.
Example 2 (Check All Button)

php_mysql_checkbox3.php


<html>
<head>
<title>ShotDev.Com Tutorial</title>
</head>
<body>
<script language="JavaScript">
function ClickCheckAll(vol)
{

var i=1;
for(i=1;i<=document.frmMain.hdnCount.value;i++)
{
if(vol.checked == true)
{
eval("document.frmMain.chkDel"+i+".checked=true");
}
else
{
eval("document.frmMain.chkDel"+i+".checked=false");
}
}
}

function onDelete()
{
if(confirm('Do you want to delete ?')==true)
{
return true;
}
else
{
return false;
}
}
</script>
<form name="frmMain" action="php_mysql_checkbox2.php" method="post"  OnSubmit="return onDelete();">
<?
$objConnect = mysql_connect("localhost","root","root") or  die(mysql_error());
$objDB = mysql_select_db("mydatabase");
$strSQL = "SELECT * FROM customer";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
?>
<table width="600" border="1">
<tr>
<th width="91"> <div align="center">CustomerID  </div></th>
<th width="98"> <div align="center">Name  </div></th>
<th width="198"> <div align="center">Email  </div></th>
<th width="97"> <div align="center">CountryCode  </div></th>
<th width="59"> <div align="center">Budget  </div></th>
<th width="71"> <div align="center">Used  </div></th>
<th width="30"> <div align="center">
<input name="CheckAll" type="checkbox" id="CheckAll" value="Y"  onClick="ClickCheckAll(this);">
</div></th>
</tr>
<?
$i = 0;
while($objResult = mysql_fetch_array($objQuery))
{
$i++;
?>
<tr>
<td><div  align="center"><?=$objResult["CustomerID"];?></div></td>
<td><?=$objResult["Name"];?></td>
<td><?=$objResult["Email"];?></td>
<td><div  align="center"><?=$objResult["CountryCode"];?></div></td>
<td align="right"><?=$objResult["Budget"];?></td>
<td align="right"><?=$objResult["Used"];?></td>
<td align="center"><input type="checkbox" name="chkDel[]"  id="chkDel<?=$i;?>"  value="<?=$objResult["CustomerID"];?>"></td>
</tr>
<?
}
?>
</table>
<?
mysql_close($objConnect);
?>
<input type="submit" name="btnDelete" value="Delete">
<input type="hidden" name="hdnCount" value="<?=$i;?>">
</form>
</body>
</html>

Screenshot

PHP MySQL Delete Multiple Record Using Checkbox
.
.
.

Download this script.
Download

1 Star2 Stars3 Stars4 Stars5 Stars6 Stars7 Stars8 Stars9 Stars10 Stars (1 votes, average: 1.00 out of 10)
Loading ... Loading ...

Leave a Reply

You must be logged in to post a comment.