羅德興老師的教學歷程檔案 - 111-2 三甲 ASP & DB - Unit 3- MySQL-RADIO-PHP-新增-查詢 |
|
|
Unit 3- MySQL-RADIO-PHP-新增-查詢Unit 3- MySQL-RADIO-PHP-新增-查詢壹、建立 www/mydb/radio.sql 儲存建立資料庫與表格的命令 DROP DATABASE IF EXISTS mydb; CREATE DATABASE mydb DEFAULT CHARACTER SET utf8; USE mydb; CREATE TABLE stmd ( field_1 CHAR(4) COMMENT '甲班、乙班、丙班、丁班', field_2 CHAR(3) COMMENT '1號、2號、3號、4號', field_3 CHAR(4) COMMENT '陳生、林生、張生、謝生', field_4 CHAR(4) COMMENT '台北、新北、台中、高雄', primary key(field_1,field_2,field_3,field_4) ); 貳、建立PHP程式 www/mydb/radio.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結合-RADIO-新增-查詢-範例</title> <link rel='stylesheet' type='text/css' href='style.css'> </head><body><center> <table> <form method='post' action=''> <tr class='alt0'><td colspan=2>學號 107XXXX 的 MySql與PHP結合-RADIO-新增-查詢-範例</td></tr> <tr><td class='alt1'>班級</td><td> <input type='radio' name='field_1' value='甲班'/>甲班 <input type='radio' name='field_1' value='乙班'/>乙班 <input type='radio' name='field_1' value='丙班'/>丙班 <input type='radio' name='field_1' value='丁班'/>丁班 </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) { $field_1=trim($_POST["field_1"]); // 取得輸入的班級,移除前後空白 if ($field_1=="") $field_1="%"; // 查詢全部 else $field_1="%".$field_1."%"; // 查詢匹配資料 $sql="select * from stmd where field_1 like '$field_1' order by field_1"; $result=mysqli_query($conn,$sql); echo "<html><head><title>MySql與PHP結合-RADIO-新增-查詢-範例</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)) { $field_1=$myrow["field_1"]; $bgcolor=$cnt % 2+ 2; echo "<tr class='alt$bgcolor'><td>$field_1</td></tr>"; $cnt++; } echo "</table></center></body></html>"; } function display_insert_page($conn) { $field_1=$_POST["field_1"]; if ($field_1=="") { display_first_page($conn); exit(); } $sql="insert into stmd values('$field_1','','','')"; mysqli_query($conn,$sql); } ?> 貳 (補充)、建立PHP程式 www/mydb/radio3.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); mysql_close($conn); function display_first_page($conn) { echo "<html><head><title>MySql與PHP結合-RADIO-新增-查詢-範例</title> <link rel='stylesheet' type='text/css' href='style.css'> </head><body><center> <table> <form method='post' action=''> <tr class='alt0'><td colspan=2>學號 107XXXX 的 MySql與PHP結合-RADIO-新增-查詢-範例</td></tr> <tr><td class='alt1'>班級</td><td> <input type='radio' name='field_1' value='甲班'/>甲班 <input type='radio' name='field_1' value='乙班'/>乙班 <input type='radio' name='field_1' value='丙班'/>丙班 <input type='radio' name='field_1' value='丁班'/>丁班 </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) { $field_1=trim($_POST["field_1"]); // 取得輸入的班級,移除前後空白 if ($field_1=="") $field_1="%"; // 查詢全部 else $field_1="%".$field_1."%"; // 查詢匹配資料 $sql="select * from stmd3 where field_1 like '$field_1' order by field_1"; //$result=mysqli_query($conn,$sql); $result=mysql_query($sql,$conn); echo "<html><head><title>MySql與PHP結合-RADIO-新增-查詢-範例</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=mysql_fetch_array($result)) { // while ($myrow=mysqli_fetch_array($result)) { $field_1=$myrow["field_1"]; $bgcolor=$cnt % 2+ 2; echo "<tr class='alt$bgcolor'><td>$field_1</td></tr>"; $cnt++; } echo "</table></center></body></html>"; } function display_insert_page($conn) { $field_1=$_POST["field_1"]; if ($field_1=="") { display_first_page($conn); exit(); } $sql="insert into stmd3 values('$field_1','','','')"; // mysqli_query($conn,$sql); mysql_query($sql,$conn); } function display_modify_page($conn) { $student_no=$_POST["student_no"]; if ($student_no=="") { display_first_page($conn); exit(); } $sql="select * from stmd2 where student_no='$student_no' "; $result=mysql_query($sql,$conn); $myrow=mysql_fetch_array($result); $depart_class=$myrow["depart_class"]; $student_no=$myrow["student_no"]; $student_name=$myrow["student_name"]; $address=$myrow["address"]; 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=6>MySql與PHP結合-TEXT-新增-查詢-更新-刪除範例</td></tr> <table> <form method='post' action=''> <tr class='alt0'><td colspan=2>MySql與PHP結合-TEXT-RADIO-新增-查詢-範例</td></tr> <tr><td class='alt1'>班級代碼</td> <td><input type='text' name='depart_class' value='$depart_class' /></td></tr> <tr><td class='alt1'>學號</td> <td><input type='text' name='student_no' value='$student_no' readonly /></td></tr> <tr><td class='alt1'>姓名</td> <td><input type='text' name='student_name' value='$student_name' /></td></tr> <tr><td class='alt1'>地址</td> <td><input type='text' name='address' value='$address' /></td></tr> </table> <input class='cmd1' type='submit' name='command' value='更新確認' onclick=\"return confirm('確定要更新嗎');\" > <input class='cmd' type='submit' name='command' value='返回'> </form></center></body></html>"; } function display_delete_page($conn) { $student_no=$_POST["student_no"]; if ($student_no=="") { display_first_page($conn); exit(); } $sql="delete from stmd2 where student_no='$student_no'"; mysql_query($sql,$conn); } function display_confirm_page($conn) { $student_no=$_POST["student_no"]; $depart_class=$_POST["depart_class"]; $student_name=$_POST["student_name"]; $gender=$_POST["gender"]; $address=$_POST["address"]; $sql="update stmd2 set depart_class='$depart_class',student_name='$student_name', address='$address' where student_no='$student_no'"; mysql_query($sql,$conn); } 參、作業 擴充radio.php程式,增加另外三個欄位 'field_2(學號)', 'field_3(姓名)', 'field_4(地址)', ,並在查詢結果的頁面中增加一個命令按鈕 '返回',其功能是 回到第一個頁面
|
|
中華科技大學數位化學習歷程 - 意見反應 |