PHP implements batch Delete (encapsulation) and php batch Delete

Source: Internet
Author: User

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);

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.