next up previous contents
Next: 實機操作練習題 Up: PHP Query 紀錄詢問 Previous: 更新紀錄   Contents   DYWANG_HOME

刪除紀錄

  1. 撰寫 deleterecord.php,刪除 tblphp 資料表的紀錄。
    [root@kvm8 ~]# vim /var/www/html/deleterecord.php
    [root@kvm8 ~]# cat /var/www/html/deleterecord.php
    <?php
    $dbhost = 'localhost:3306';
    $dbuser = 'root';
    $dbpass = '123qwe';
    $conn = mysqli_connect($dbhost, $dbuser, $dbpass) 
    	or die(mysqli_connect_error().PHP_EOL);
    mysqli_select_db( $conn, 'dywphp' )
    	or die('Error: '.mysqli_error($conn).PHP_EOL);
    $sql = "delete from tblphp where id=1";
    $retval = mysqli_query( $conn, $sql )
    	or die('Error: '.mysqli_error($conn).PHP_EOL);
    echo "Data Deleted successfully\n";
    mysqli_close($conn); 
    ?>
    
  2. 先修改 selectrecord.php,將 where 條件移除。
    [root@kvm8 ~]# grep '^\$sql' /var/www/html/selectrecord.php
    $sql = "select id, title, author,submission_date from tblphp";
    
  3. curl 連上網址 http://kvm8.deyu.wang/selectrecord.php,管線處理將 html 換行標籤 <br> 改成 shell 的換行符號 \n,網頁上列出資料表 tblphp 所有紀錄。
    [root@kvm8 ~]# curl -s http://kvm8.deyu.wang/selectrecord.php | sed 's/<br> */\n/g'
    Dywphp ID :1  
    Title: python 
    Author: David 
    Submission Date : 0000-00-00 
    --------------------------------
    Dywphp ID :2  
    Title: python 
    Author: dywang 
    Submission Date : 2020-01-29 
    --------------------------------
    Fetched data successfully
    
  4. curl 連上網址 http://kvm8.deyu.wang/deleterecord.php,管線處理將 html 換行標籤 <br> 改成 shell 的換行符號 \n,網頁回應 'Data Deleted successfully'。
    [root@kvm8 ~]# curl -s http://kvm8.deyu.wang/deleterecord.php | sed 's/<br> */\n/g'
    Data Deleted successfully
    
  5. curl 再連上網址 http://kvm8.deyu.wang/selectrecord.php,管線處理將 html 換行標籤 <br> 改成 shell 的換行符號 \n,網頁上列出資料表 tblphp 所有紀錄,id=1 的紀錄已不存在。
    [root@kvm8 ~]# curl -s http://kvm8.deyu.wang/selectrecord.php | sed 's/<br> */\n/g'
    Dywphp ID :2  
    Title: python 
    Author: dywang 
    Submission Date : 2020-01-29 
    --------------------------------
    Fetched data successfully
    



De-Yu Wang 2020-04-07