<?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_modify_page($conn);
}
elseif ($command=="更新確認") {
display_confirm_page($conn);
display_first_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='depart_class' size=10/></td></tr>
<tr><td class='alt1'>學號</td>
<td><input type='text' name='student_no' size=20 /></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) {
$depart_class=$_POST["depart_class"];
if ($depart_class=="") $depart_class="%";
else $depart_class="%".$depart_class."%";
$student_no=trim($_POST["student_no"]);
if ($student_no=="") $student_no="%";
else $student_no="%".$student_no."%";
$sql="select * from stmd where depart_class like '$depart_class' and
student_no like '$student_no' order by depart_class,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=3>MySql與PHP結合-TEXT-新增-查詢-刪除-更新範例</td></tr>
<tr class='alt1'><td>班級代碼</td><td>學號</td><td>選擇</td></tr>";
$cnt=0;
while ($myrow=mysqli_fetch_array($result)) {
$depart_class=$myrow["depart_class"];
$student_no=$myrow["student_no"];
$bgcolor=$cnt % 2+ 2;
echo "<tr class='alt$bgcolor'><td>$depart_class</td><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='更新'>
<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) {
$depart_class=trim($_POST["depart_class"]);
$student_no=trim($_POST["student_no"]);
if ($student_no=="") {
display_first_page($conn); exit();
}
$sql="insert into stmd values('$depart_class','$student_no','','')";
mysqli_query($conn,$sql);
}
function display_modify_page($conn) {
$student_no=$_POST["student_no"];
if ($student_no=="") {
display_first_page($conn); exit();
}
$sql="select * from stmd where student_no='$student_no' ";
$result=mysqli_query($conn,$sql);
$myrow=mysqli_fetch_array($result);
$depart_class=$myrow["depart_class"];
$student_no=$myrow["student_no"];
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>
</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 stmd where student_no='$student_no'";
mysqli_query($conn,$sql);
}
function display_confirm_page($conn) {
$student_no=$_POST["student_no"];
$depart_class=$_POST["depart_class"];
$sql="update stmd set depart_class='$depart_class'
where student_no='$student_no'";
mysqli_query($conn,$sql);
}
?>
陸、作業