PHP批量刪除數(shù)據(jù)減輕程序員壓力
PHP程序員們?cè)谔幚砭W(wǎng)站中垃圾文件數(shù)據(jù)的時(shí)候,通常都會(huì)遇到大量的數(shù)據(jù)刪除需求。那么我們就想到了PHP批量刪除數(shù)據(jù)的方法。#t#
首先在文章列表頁(yè)面(list.php),將多選筐命名為:“$del_id[]”,值為文章ID號(hào)。 例如(list.php):
- < form name='del_form' action='del.php'
method='post'> - < ?php
- $result=mysql_query('select * from news');
- while($rs=mysql_fetch_array($result)){
- ?>
- < input name='del_id[]' type='checkbox'
id='del_id[]' value='< ?=$rs[id]?>' /> - < ?=$rs[title]?>
- < ?php
- }
- ?>
- < /form>
- 2、處理頁(yè)面(del.php):
- < ?php
- if($del_id!=''){
- $del_num=count($del_id);
- for($i=0;$i< $del_num;$i++){
- mysql_query('Delete from news where
id='$del_id[$i]''); - }
- echo('< script type='text/javascript'>
alert('刪除成功!');history.back();< /script>'); - }else{
- echo('< script type='text/javascript'>
alert('請(qǐng)先選擇項(xiàng)目!');history.back();< /script>'); - }
- ?>
1.PHP批量刪除數(shù)據(jù)首先引入jquery腳本庫(kù)
- < script language='JavaScript'
type='text/javascript'
src='http://jqueryjs.googlecode.
com/files/jquery-1.3.2.min.js'>- < /script>
將下列代碼加上
- < script language='JavaScript'
type='text/javascript'>- $(document).ready(function()
- {
- $('input[id='del']').click(function()
- {
- var del_arry='';
- $('input[name='del_id[]'][checked]')
.each(function(){- del_arrydel_arry=del_arry+$(this).val()+',';
- });
- if (del_arry!='')
- {
- $.post('com_del.php', {value:''+
del_arry+''}, function(data){- if(data==1) {
- window.location.reload();
- alert(' 刪除成功!');
- }else if(data==2){
- alert('刪除失?。?);
- }
- });
- }
- });
- });
- < /script>
2.com_shen.php(下面的部分可以自己發(fā)揮,用你自己的數(shù)據(jù)庫(kù)連接和處理方法)
- < ?php
- include '../cyr/inc/conn.php';
- include '../cyr/inc/page.class.php';
- $conn = new DB();
- $conn-> Connection();
- $array = $_POST['value'];
- //將所選的值組成的數(shù)組放入$array數(shù)組
- $array=split (',', $array);
- if(!empty($array)){
- $del_num=count($array);
- for($ii=0;$ii< $del_num;$ii++){
- $conn->str = 'update yj_comment set
co_shenhe=1 where id='.$array[$ii];- $conn->getResult();
- }
- echo $string = 1;
- }else{
- echo $string =2;
- }
- $conn->ColseConnection();
- ?>
以上就是PHP批量刪除數(shù)據(jù)的相關(guān)實(shí)現(xiàn)方法。