php部分---對資料的多條件查詢,大量刪除

來源:互聯網
上載者:User

標籤:title   定義   val   click   i++   post   串連失敗   部分   where   

1.給多條件查詢,添加條件

<h1>新聞資訊查看</h1><?php$tiaojian1=" 1=1";$tiaojian2=" 1=1";$title="";          //為了下面foreach中能夠取到這個變數,所以在外邊定義一下。  $author="";if(!empty($_POST[‘title‘])){    $title=$_POST[‘title‘];    $tiaojian1=" title like ‘%{$title}%‘";    }    /*else    {        echo "<script> alert(‘請輸入‘);</script>";    //如果輸入的是空,可以做一個彈窗進行提示        }*/if(!empty($_POST[‘author‘])){    $author=$_POST[‘author‘];    $tiaojian2=" author like ‘%{$author}%‘";    }?>

2.建立表單

<div><form action="" method="post">標題:<input type="text" name="title" value="<?php echo $title; ?>"><input type="text" name="author" value="<?php echo $author; ?>">  <!--輸入關鍵字後文字框中,需要給文字框添加一個value值,value="<?php echo $title; ?>"--><input type="submit" value="查詢"></form><form action="fabu.php"><input type="submit" value="添加新聞"></form></div> 

 

3.建立顯示地區

<form action="piliangshanchu.php" method="post"><table width="95%" border="1" cellpadding="0" cellspacing="0" align="center"><tr><td><input type="checkbox" onclick="quanxuan(this)">標題</td>  <!--在標題列添加一個複選框--><td>作者</td><td>來源</td><td>內容</td><td>發布時間</td><td>刪除</td><td>修改</td></tr><?php$db=new MySQLi("localhost","root","123","test1");mysqli_connect_error()?"串連失敗":"";$sql="select * from news where {$tiaojian1} and {$tiaojian2}";echo $sql;                                                 //輸出語句看看結果$result=$db->query($sql);$attr=$result->fetch_all();foreach($attr as $v){    /*$str=str_replace($title,"<mark>{$title}</mark>",$v[1]);  //str_replace進行替換,添加mark標籤,對關鍵字進行特殊顯示*/    $str=str_replace($title,"<span style=‘color:red;‘>{$title}</span>",$v[1]);    //還可以通過添加<span>標籤,改span的color    echo "<tr><td><input type=‘checkbox‘ value=‘{$v[0]}‘ name=‘sc[]‘ class=‘xuanzhong‘/>{$str}</td><td>{$v[2]}</td><td>{$v[3]}</td><td>{$v[4]}</td><td>{$v[5]}</td><td><a href=‘shanchu.php?ids={$v[0]}‘ onclick=\"return confirm(‘確認刪除嘛‘)\">刪除</a></td><td><a href=‘xiugai.php?ids={$v[0]}‘>修改</a></td></tr>";        //重點:進行複選框多選時候,name的值要用sc[]數組接收    }?></table><div><input type="submit" value="大量刪除"></div></form>

 

4.通過標題列的複選框狀態,對下面的複選框進行多選,用js

<script type="text/javascript">function quanxuan(a){    var ck=document.getElementsByClassName("xuanzhong");    for(var i=0;i<ck.length;i++)    {        if(a.checked)            //判斷a的選中狀態        {        ck[i].setAttribute("checked","checked");//設定添加屬性        }        else        {            ck[i].removeAttribute("checked");//移除屬性            }    }            }</script>

5.大量刪除的處理介面

<?phpif(!empty($_POST[‘sc‘]))    //通過上面複選框的選中值,進行提取,提取出來是數組的形式{$attr=$_POST[‘sc‘];          //放到一個數組中$db=new MySQLi("localhost","root","123","test1");mysqli_connect_error()?"串連失敗":"";/*foreach($attr as $v){$sql="delete from news where ids=‘{$v}‘";if($result=$db->query($sql)){    header("location:main.php");    }    else    {        echo "刪除失敗";        }}*/$atr=implode("‘,‘",$attr);            //通過implode函數,把數組合并成一個字串,為下面的sql語句形成條件陳述式   條件陳述式in(‘‘,‘‘,‘‘,‘‘)這樣的形式$sql="delete from news where ids in(‘{$atr}‘)";     if($result=$db->query($sql)){    header("location:main.php");    }    else    {        echo "刪除失敗";        }}

 

php部分---對資料的多條件查詢,大量刪除

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.