羅德興老師的教學歷程檔案 - 112-2 資料庫管理系統 (DBMS) - Unit 4- MySQL-TEXT-PHP-新增-查詢-刪除
 

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


歷程檔案 Portfolio

    Unit 4- MySQL-TEXT-PHP-新增-查詢-刪除

    Unit 4- MySQL-TEXT-PHP-新增-查詢-刪除

    壹、建立 www/mydb來存放所有的開發文件
     
    貳、建立 www/mydb/text4.sql 儲存建立資料庫與資料表的命令
     
    DROP DATABASE IF EXISTS mydb; 
    CREATE DATABASE mydb DEFAULT CHARACTER SET utf8; 
    USE mydb;  
    CREATE TABLE stmd (
      depart_class   CHAR(6)     COMMENT  '班級代碼',
      student_no     CHAR(9)     COMMENT  '學號',
      student_name   VARCHAR(20) COMMENT  '姓名',
      gender         CHAR(4)     COMMENT  '性別',
      address        VARCHAR(60) COMMENT  '地址',
      primary key(student_no)
    );
     
     
    參、建立 www/mydb/style.css
     
    <style type='text/css'>
       body { width:100%; margin:0 padding:0; font-family:標楷體; }
       table { border-collapse:collapse; border:1px solid black;
               empty-cells:show; width:100%; }
       th { background-color:ccffff; border: 1px solid; font-family:標楷體;}
       td { border: 1px solid; text-align : center; font-family:標楷體; }
       .alt0 { background-color:#99ffff; font-weight:bold; font-family:標楷體; }
       .alt1 { background-color:#ccccff; font-family:標楷體; }
       .alt2 { background-color:#ccffff; font-family:標楷體; }
       .alt3 { background-color:#fff8c6; font-family:標楷體; }
     
       a: { background:ccccff; border:1px solid ccc; color:000;
           padding:.3em .5em;  margin-top:1px;  margin-bottom:1px; text-align:center;
           text-decoration:none; display: inline-block; font-family:標楷體; }
       a:link {background-color:ff88ee } 
       a:visited {background-color:FFFF85;}
       a:hover {background-color:FF704D; }  
       a:active {background-color:FF704D; } 
       p {background-color: #81F781; display:inline;font-family:標楷體; font-weight:bold;}  
       .cmd  {font-family:標楷體; font-size:18px; background-color:ccccff; width:4em; } 
       .cmd1 {font-family:標楷體; font-size:18px; background-color:ccccff; width:10em; } 
       .cmd2 {font-family:標楷體; font-size:18px; background-color:ccccff; width:5em; } 
     </style>
     
    肆、建立 www/mydb/config.php
     
    <?php
      $host = "127.0.0.1";
      $user = "root";
      $pwd = "rootroot";
      $dbname="mydb";
      $conn=mysqli_connect($host,$user,$pwd) or die("無法連接主機");
      mysqli_query($conn,'SET NAMES utf8'); 
      mysqli_select_db($conn,$dbname) or die("無法連接資料庫");
    ?>
     
    伍、建立PHP程式 www/mydb/unit4.php

    <?php
     
      include "config.php"; // 連線組態
      if (isset($_POST["command"])) $command=$_POST["command"];
      else $command="";
      if ($command=="" || $command=="返回") {
        display_first_page($conn);
      }
      elseif ($command=="新增") {
        display_insert_page($conn);
        display_first_page($conn);
      }
      elseif ($command=="查詢") {
        display_search_page($conn);
      }
      elseif ($command=="刪除") {
        display_delete_page($conn);
        display_first_page($conn);
      }
      mysqli_close($conn);
     
      function display_first_page($conn) { // 第一個頁面
        echo "<html><head><title>MySql與PHP結合-TEXT-新增-查詢-刪除-範例</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結合-TEXT-新增-查詢-刪除-範例</td></tr>
              <tr><td class='alt1'>學號</td>
              <td><input type='text' name='student_no' /></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=trim($_POST["student_no"]); // 取得輸入的學號,移除前後空白
        if ($student_no=="") $student_no="%";   // 查詢全部
        else $student_no="%".$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結合-TEXT-新增-查詢-範例</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><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>
                <td><input type='radio' name='student_no' value='$student_no'></td></tr>";
          $cnt++;
        }
        echo "</table>
              <input class='cmd' type='submit' name='command' value='刪除'
                onclick=\"return confirm('確定要刪除嗎');\" >
              <input class='cmd' type='submit' name='command' value='返回'>
              </form></center></body></html>";
      }
     
      function display_insert_page($conn) {
        $student_no=$_POST["student_no"];
        if ($student_no=="") {
          display_first_page($conn); exit();
        }
        $sql="insert into stmd values('','$student_no','','','')";
        mysqli_query($conn,$sql);                    
      }
     
      function display_delete_page($conn) {
        $student_no=$_POST["student_no"];
        if ($student_no=="") {
          display_first_page($conn); exit();
        }
        $sql="delete from stmd where student_no='$student_no'";
        mysqli_query($conn,$sql);                    
      }
    ?>
     
     
    陸、作業
     
     擴充 unit4.php程式,增加另外四個個欄位 '班級代碼', '姓名',性別,  地址', 可以做新增, 查詢, 刪除

    班級代碼欄位請用 Radio,選項有 四技資管一甲 四技資管二甲  四技資管三甲 四技資管四甲

    性別欄未使用 Radio 選項有 男 女
    全部共 0則留言
    登入帳號密碼代表遵守學術網路規範


    文章分類 Labels


    最新文章 Top10

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