PHP implements batch Delete (encapsulation) and php batch Delete
Front-end
<! DOCTYPE html> // Check box
Function checkall (all) {var ck = document. getElementsByClassName ("ck"); if (all. checked) {for (var I = 0; I <ck. length; I ++) {ck [I]. setAttribute ("checked", "checked") ;}} else {for (var I = 0; I <ck. length; I ++) {ck [I]. removeAttribute ("checked") ;}}</script> <form action = "test. php "method =" post "> <table border =" 1 "> <tr> <th> <input type =" checkbox "name =" all "onclick =" checkall (this) "/> id </th> <th> name </th> </tr> <! -- Call the display list function here --> <? Php show ()?> <Tr> <td colspan = "3"> <input type = "submit" value = "batch Delete"> </td> </tr> </table> </form> </body> <? Php // display the list function show () {// connect to the Database @ mysql_connect ('localhost', 'root', ''); mysql_select_db ('test '); mysql_query ('set names utf8'); $ SQL = "select id, name from test"; $ res = mysql_query ($ SQL ); // cyclically retrieve data while ($ row = mysql_fetch_row ($ res )) {echo "<tr> <td> <input type = 'checkbox' value = '{$ row [0]}' name = 'item [] 'class = 'ck '/ >{$ row [0] }</td> <td >{$ row [1] }</td> </tr> ";}}?> </Html>
Background
<? Php // receives an array from post $ arr =$ _ POST ["item"];/*** batch Delete * Train of Thought: Put the data selected by the foreground in batches in the array, delete the array. * @ param $ arr <array ()> * @ return $ res success or failure */function batch_del ($ arr) {@ mysql_connect ('localhost ', 'root', ''); mysql_select_db ('test'); mysql_query ('set names utf8'); // combines array elements into strings: $ str = implode ("','", $ arr );
// In indicates multiple $ SQL = "delete from test where id in ('{$ str}')"; $ res = mysql_query ($ SQL); if (! $ Res) {echo "deletion failed";} else {if (mysql_affected_rows ()> 0) {echo "deletion successful ";} else {echo "No rows affected" ;}}// call the batch delete function
batch_del($arr);