前臺
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
<!DOCTYPE html> < html > < head > < title >批量刪除</ title > </ head > < body > < script type = "text/javascript" > //復選框 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 >名字</ th ></ tr > <!-- 此處調用顯示列表函數 --> <? php show() ?> < tr >< td colspan = "3" >< input type = "submit" value = "批量刪除" ></ td ></ tr > </ table > </ form > </ body > <? php //顯示列表 function show() { //連接數據庫 @mysql_connect('localhost','root',''); mysql_select_db('test'); mysql_query('set names utf8'); $ sql = "select id,name from test" ; $ res = mysql_query ($sql); //循環取出數據 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 > |
后臺
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
<?php //接收post傳來的數組 $arr = $_POST [ "item" ]; /** * 批量刪除 * 思路:把前臺批量選擇的數據放在數組里,刪除該數組即可 * @param $arr <array()> * @return $res 成功or失敗 */ function batch_del( $arr ) { @mysql_connect( 'localhost' , 'root' , '' ); mysql_select_db( 'test' ); mysql_query( 'set names utf8' ); //把數組元素組合為字符串: $str = implode( "','" , $arr ); //in 表示多個 $sql = "delete from test where id in('{$str}')" ; $res = mysql_query( $sql ); if (! $res ){ echo "刪除失敗" ; } else { if (mysql_affected_rows()>0){ echo "刪除成功" ; } else { echo "沒有行受到影響" ; } } } //調用批量刪除函數 batch_del( $arr ); |
以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持服務器之家!
原文鏈接:http://www.cnblogs.com/zxf100/p/6748834.html