How to use PHP & SQL Server (mssql) Delete Multiple Rows Record Using Checkbox Learn / tutorial php programming how to using PHP delete multiple record from SQL Server database.
ShotDev Focus:
- PHP & SQL Server delete multiple record using checkbox.
Example 1
php_sqlserver_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_sqlserver_checkbox2.php" method="post" OnSubmit="return onDelete();"> <? $objConnect = mssql_connect("localhost","sa",""); $objDB = mssql_select_db("mydatabase"); $strSQL = "SELECT * FROM customer"; $objQuery = mssql_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 = mssql_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> <? mssql_close($objConnect); ?> <input type="submit" name="btnDelete" value="Delete"> </form> </body> </html>
php_sqlserver_checkbox2.php
<html> <head> <title>ShotDev.Com Tutorial</title> </head> <body> <? $objConnect = mssql_connect("localhost","sa",""); $objDB = mssql_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 = mssql_query($strSQL); } } echo "Record Deleted."; mssql_close($objConnect); ?> </body> </html>
Create a php file and save to path root-path/myphp/
Run
http://localhost/myphp/php_sqlserver_checkbox1.php
Screenshot
Example 2 (Check All Button)
php_sqlserver_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_sqlserver_checkbox2.php" method="post" OnSubmit="return onDelete();"> <? $objConnect = mssql_connect("localhost","sa",""); $objDB = mssql_select_db("mydatabase"); $strSQL = "SELECT * FROM customer"; $objQuery = mssql_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 = mssql_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> <? mssql_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_sqlserver_checkbox3.php
Screenshot