php頁面中實現對資料庫的操作

來源:互聯網
上載者:User

標籤:fetch   nts   str   sid   庫存   header   全選   java   var   

  對頁面內容進行提交,添加到資料庫:

<?php//var_dump($_POST);$title = $_POST["title"];$Author = $_POST["Author"];$source = $_POST["source"];$content = $_POST["content"];$time = time();$db = new MySQLi("localhost","root","123","newssystem");$sql = "insert into news values(0,‘{$title}‘,‘{$Author}‘,‘{$source}‘,‘{$content}‘,now())";echo $sql;$result = $db->query($sql);if($result){    echo "<script type=‘text/javascript‘>window.location.href=‘fabuxinwen.php‘;</script>";}else{    echo "添加失敗!";}

 

  在網頁中顯示資料庫裡面的內容:

//首先在頁面內添加一個表格,並設定列名<table class="table table-bordered">  <thead>    <tr>      <th>id</th>      <th>title</th>      <th>author</th>      <th>source</th>      <th>date</th>      <th>update</th>      <th>delete</th>    </tr>  </thead>//表身內容為資料庫儲存的內容,對其進行展示  <tbody>     <?php    $db = new MySQLi("localhost","root","123","newssystem");    $sql = "select * from news";    $result = $db->query($sql);    if($result){        $arr = $result->fetch_all();        foreach($arr as $v){            echo "<tr>      <td>{$v[0]}</td>      <td>{$v[1]}</td>      <td>{$v[2]}</td>      <td>{$v[3]}</td>      <td>{$v[5]}</td>      <td>          <a href=‘xiugai.php?newsid={$v[0]}‘ ><button type=‘button‘ class=‘btn btn-primary btn-sm‘>修改</button></a>      </td>      <td>          <a href=‘shanchu.php?title={$v[1]}‘ onclick=\"return confirm(‘確認刪除嗎?‘)\">
        <button type=‘button‘ class=‘btn btn-primary btn-sm‘>刪除</button>
      </a> </td> </tr>"; } } ?> </tbody></table>

 

  對資料進行刪除:

<?php$title = $_GET["title"];$db = new MySQLi("localhost","root","123","newssystem");$sql = "delete from news where title=‘{$title}‘";$result = $db->query($sql);if($result){    header("location:chakan.php");}else{    echo "刪除失敗!";}

    如果實現大量刪除:

      首先在列表每一行最前面加上一個複選框:

 <td><input type=‘checkbox‘ class=‘ck‘ name=‘ck[]‘ value=‘{$v[0]}‘/>{$v[0]}</td>

      然後在列表列的最前面加入一個可以全選的複選框:

<input type="checkbox" id="ckall" />

      加入JS代碼實現全選:

<script type="text/javascript">var ckall = document.getElementById("ckall");ckall.onclick = function(){    var xz = ckall.checked;    var ck = document.getElementsByClassName("ck");    for(var i=0;i<ck.length;i++){        ck[i].checked = xz;     }}</script>

      加入大量刪除按鈕,然後使其可以運行下一個頁面的代碼:

<?php$arr = $_POST["ck"];//delete from info where code in(‘p001‘,‘p002‘,‘p003‘)$str = implode("‘,‘",$arr);$sql = "delete from info where code in(‘{$str}‘)";$db = new MySQLi("localhost","root","123","mydb");$result = $db->query($sql);if($result){    header("location:main.php");}else{    echo "刪除失敗!";}

      即可實現大量刪除功能。

 

  對資料修改(需要同時運用到添加和刪除):

    首先先將要修改的資料展示出來:

//在要展示的修改的頁面加入一段php代碼<?php//取出主索引值$newsid = $_GET["newsid"];//讀取該條資料$db = new MySQLi("localhost","root","123","newssystem");$sql = "select * from news where newsid={$newsid}";$result = $db->query($sql);$arr1 = $result->fetch_row();?>

    然後在每一條資料內使其內容顯示出來(給文字框賦值),如:<?php echo $arr1[0] ?>

    在建立一個只有php的頁面:

<?php$newsid = $_POST["newsid"];$title = $_POST["title"];$Author = $_POST["Author"];$source = $_POST["source"];$content = $_POST["content"];$db = new MySQLi("localhost","root","123","newssystem");$sql = "update news set title=‘{$title}‘,Author=‘{$Author}‘,source=‘{$source}‘,content=‘{$content}‘ where newsid = {$newsid}";$result = $db->query($sql);if($result){    header("location:chakan.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.