羅德興老師的教學歷程檔案 - 112-2 資料庫管理系統 (DBMS) - Unit 2-MySQL-TEXT-PHP-新增-查詢 |
|
|
Unit 2-MySQL-TEXT-PHP-新增-查詢Unit 2-MySQL-TEXT-PHP-新增-查詢壹、建立 www/mydb來存放所有的開發文件 貳、建立 www/mydb/text.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 '姓名', 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 = "1234"; $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/text.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結合-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> <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(); } $sql="insert into stmd values('','$student_no','','')"; mysqli_query($conn,$sql); } ?> 陸、作業 擴充text.php程式,增加另外三個欄位 '班級代碼', '姓名', '地址',
|
|
中華科技大學數位化學習歷程 - 意見反應 |