羅德興老師的教學歷程檔案 - 111-2 三甲 ASP & DB - Unit 6- MySQL-CHECKBOX-PHP-新增-查詢-刪除
 

企業資訊與管理系
助理教授/日導
羅德興


歷程檔案 Portfolio

    Unit 6- MySQL-CHECKBOX-PHP-新增-查詢-刪除

     Unit 6- MySQL-CHECKBOX-PHP-新增-查詢-刪除

    壹、建立 www/mydb/check.sql 儲存建立資料庫與資料表的命令
     
    DROP DATABASE IF EXISTS mydb; 
    CREATE DATABASE mydb DEFAULT CHARACTER SET utf8; 
    USE mydb;  
    CREATE TABLE stmd (
     
      oid            INT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '遞增識別碼',
      depart_class   VARCHAR(16) COMMENT  '甲班、乙班、丙班、丁班',
      student_no     VARCHAR(16) COMMENT  '1號、2號、3號、4號',
      student_name   VARCHAR(16) COMMENT  '陳生、林生、張生、謝生',
      address        VARCHAR(16) COMMENT  '台北、新北、台中、高雄',
      primary key(oid)
    );
     
     
    貳、建立PHP程式 www/mydb/unit6.php
     
    <?php
     
      include "config.php"; // 連線組態
      if (isset($_POST["command"])) $command=$_POST["command"];
      else $command="";
      if ($command=="") {
        display_first_page($conn);
      }
      elseif ($command=="新增") {
        display_insert_page($conn);
        display_first_page($conn);
      }
      elseif ($command=="查詢") {
        display_search_page($conn);
      }
      mysqli_close($conn);
     
      function display_first_page($conn) { // 第一個頁面
        echo "<html><head><title>MySql與PHP結合-CHECK-新增-查詢-範例</title>
              <link rel='stylesheet' type='text/css' href='style.css'>
              </head><body><center>
              <table> <form method='post' action=''>
              <tr class='alt0'><td colspan=2>MySql與PHP結合-CHECK-新增-查詢-範例</td></tr>
              <tr><td class='alt1'>學號</td><td>
              <input type='checkbox' name='student_no[]' value='1號'/>1號
              <input type='checkbox' name='student_no[]' value='2號'/>2號
              <input type='checkbox' name='student_no[]' value='3號'/>3號
              <input type='checkbox' name='student_no[]' value='4號'/>4號
              </td></tr></table>
              <input class='cmd' type='submit' name='command' value='查詢'>
              <input class='cmd' type='submit' name='command' value='新增'>
              </form></center></body></html>";
      }
     
      function display_search_page($conn) {
        $student_no=$_POST["student_no"];
        if ($student_no=="") $student_no="%";   // 查詢全部
        else $student_no="%".implode(",",$student_no)."%";   // 查詢匹配的學號
        $sql="select * from stmd where student_no like '$student_no' order by student_no";
        $result=mysqli_query($conn,$sql);
        echo "<html><head><title>MySql與PHP結合-CHECK-新增-查詢-範例</title>
              <link rel='stylesheet' type='text/css' href='style.css'>
              </head><body><center>
              <table><form method='post' action=''>
              <tr class='alt0'><td colspan=5>MySql與PHP結合-TEXT-新增-查詢-範例</td></tr>
              <tr class='alt1'><td>學號</td></tr>";
        $cnt=0;
        while ($myrow=mysqli_fetch_array($result)) { 
          $student_no=$myrow["student_no"];    
          $bgcolor=$cnt % 2+ 2;
          echo "<tr class='alt$bgcolor'><td>$student_no</td></tr>";
          $cnt++;
        }
        echo "</table></center></body></html>"; 
      }
     
      function display_insert_page($conn) {
        $student_no=$_POST["student_no"];
        if ($student_no=="") {
          display_first_page($conn); exit();
        } 
        else $student_no=implode(",",$student_no);
        $sql="insert into stmd values(null,'','$student_no','','')";
        mysqli_query($conn,$sql);                    
      }
     
    ?>
     
    三、作業
     
    擴充 unit6.php程式,增加另外三個欄位 '班級代碼', '姓名', '地址',
     
    程式必須做新增、查詢、刪除等功能
    全部共 0則留言
    登入帳號密碼代表遵守學術網路規範


    文章分類 Labels


    最新文章 Top10

    中華科技大學數位化學習歷程 - 意見反應