十四週PHP DELETE TABLE<?php include "config.php"; $command=$_POST["command"]; // 取得命令按鍵 if ($command=="" || $command=="返回") { // 首次進入或返回 display_first_page($conn); // $conn 為與MySQL連線 } elseif ($command=="查詢") { display_search_page($conn); } elseif ($command=="新增") { display_insert_page($conn); display_first_page($conn); } elseif ($command=="刪除") { display_delete_page($conn); display_first_page($conn); } elseif ($command=="更新") { display_modify_page($conn); display_first_page($conn); } elseif ($command=="確認") { display_confirm_page($conn); display_first_page($conn); } mysql_close($conn); function display_first_page($conn) { // 第一個頁面 echo "<html><head><title>MySql與PHP結合-查詢範例</title> <link rel='stylesheet' type='text/css' href='style.css'> </head><body><center> <table> <form method='post' action=''> <tr class='alt0'><td colspan=4>MySql與PHP結合-查詢-新增-範例</td></tr> <tr><td class='alt1'>班級代碼</td> <td><input type='text' name='depart_class' /></td> <td class='alt1'>學號</td> <td><input type='text' name='student_no' /></td></tr> <tr><td class='alt1'>學生姓名</td> <td><input type='text' name='student_name' /></td> <td class='alt1'>地址</td> <td><input type='text' name='address' /></td></tr></table> <input type='submit' name='command' value='查詢'> <input type='submit' name='command' value='新增'> </form></center></body></html>"; } function display_search_page($conn) { $depart_class=trim($_POST["depart_class"]); $student_no=trim($_POST["student_no"]); $student_name=trim($_POST["student_name"]); $address=trim($_POST["address"]); if ($depart_class=="") $depart_class="%"; // 查詢全部 else $depart_class="%".$depart_class."%"; // 查詢匹配 if ($student_no=="") $student_no="%"; // 查詢全部 else $student_no="%".$student_no."%"; // 查詢匹配 if ($student_name=="") $student_name="%"; // 查詢全部 else $student_name="%".$student_name."%"; // 查詢匹配 if ($address=="") $address="%"; // 查詢全部 else $address="%".$address."%"; // 查詢匹配 // 準備查詢命令按班級學號排序 $sql="select * from stmd where depart_class like '$depart_class' and "; $sql.="student_no like '$student_no' and student_name like '$student_name' "; $sql.="and address like '$address' order by depart_class,student_no"; $result=mysql_query($sql,$conn); // SQL命令執行 echo "<html><head><title>MySql與PHP結合-更新-刪除-範例</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結合-更新-刪除-範例</td></tr> <tr class='alt1'><td>班級</td><td>學號</td><td>姓名</td><td>地址</td><td>選擇</td></tr>"; $cnt=0; while ($myrow=mysql_fetch_array($result)) { // 取出一列 $depart_class=$myrow["depart_class"]; // 取出欄位 $student_no=$myrow["student_no"]; // 取出欄位 $student_name=$myrow["student_name"]; // 取出欄位 $address=$myrow["address"]; // 取出欄位 $bgcolor=$cnt % 2+ 2; echo "<tr class='alt$bgcolor'><td>$depart_class</td><td>$student_no</td> <td>$student_name</td><td>$address</td> <td><input type=radio name=student_no value='$student_no'></td></tr>"; $cnt++; } echo "</table> <input type='submit' name='command' value='更新'> <input type='submit' name='command' value='刪除' onclick=\"return confirm('??? 確定要刪除嗎 ???')\" > <input type='submit' name='command' value='返回'> </form></center></body></html>"; } function display_insert_page($conn) { $depart_class=$_POST["depart_class"]; $student_no=$_POST["student_no"]; $student_name=$_POST["student_name"]; $address=$_POST["address"]; if ($depart_class=="" || $student_no=="" || $student_name=="" || $address=="") { display_first_page($conn); exit(); } $sql="insert into stmd values('$depart_class','$student_no','$student_name','$address')"; mysql_query($sql,$conn); } function display_delete_page($conn) { $student_no=$_POST["student_no"]; if ($student_no=="") return; echo "student_no=$student_no<br>"; die(); // 加入刪除的程式碼 }
?>
|