How to use PHP & Access (odbc) Delete Multiple Rows Record Using Checkbox This is tutorial php developers how to using PHP delete multiple record from access database.
ShotDev Focus:
- PHP & Microsoft Access delete multiple record using checkbox.
Example 1
php_access_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_access_checkbox2.php" method="post" OnSubmit="return onDelete();"> <? $objConnect = odbc_connect("mydatabase","",""); $strSQL = "SELECT * FROM customer"; $objExec = odbc_exec($objConnect, $strSQL) or die ("Error Execute [".$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 = odbc_fetch_array($objExec)) { ?> <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> <? odbc_close($objConnect); ?> <input type="submit" name="btnDelete" value="Delete"> </form> </body> </html>
php_access_checkbox2.php
<html> <head> <title>ShotDev.Com Tutorial</title> </head> <body> <? $objConnect = odbc_connect("mydatabase","",""); for($i=0;$i<count($_POST["chkDel"]);$i++) { if($_POST["chkDel"][$i] != "") { $strSQL = "DELETE FROM customer "; $strSQL .="WHERE CustomerID = '".$_POST["chkDel"][$i]."' "; $objQuery = odbc_exec($objConnect, $strSQL); } } echo "Record Deleted."; odbc_close($objConnect); ?> </body> </html>
Create a php file and save to path root-path/myphp/
Run
http://localhost/myphp/php_access_delete1.php
Screenshot
.
.
Example 2 (Check All Button)
php_access_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_access_checkbox2.php" method="post" OnSubmit="return onDelete();"> <? $objConnect = odbc_connect("mydatabase","",""); $strSQL = "SELECT * FROM customer"; $objExec = odbc_exec($objConnect, $strSQL) or die ("Error Execute [".$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 = odbc_fetch_array($objExec)) { $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> <? odbc_close($objConnect); ?> <input type="submit" name="btnDelete" value="Delete"> <input type="hidden" name="hdnCount" value="<?=$i;?>"> </form> </body> </html>
Create a php file and save to path root-path/myphp/
Run
http://localhost/myphp/php_access_delete3.php
Screenshot