| 以PHP更新MySQL範例<?php include "config.php"; $command=$_POST["command"]; if ($command==null) { // 先顯示第一個畫面 echo(" <html> <title>MySql 更新範例</title> <center> <form method='post' action=$PHP_SELF> <table border=0> <tr> <td>學號:</td> <td> <input type='text' name='student_no' size='9'> </td> </tr> </table> <input type='submit' name='command' value='新增'> <input type='submit' name='command' value='查詢'> <input type='reset' name='command' value='清除 '> </form> </center> </html> "); } if ($command=="查詢") { // 按了查詢鈕 $student_no=$_POST["student_no"]; $sql="select * from stmd where student_no like '$student_no' order by student_no"; $result=mysql_query($sql,$conn); $no_of_rows=mysql_affected_rows($conn); if ($no_of_rows==1) { $myrow=mysql_fetch_array($result); $student_no=$myrow["student_no"]; // 將學號以hidden傳送至下一畫面 echo("<html> <body> <center> <form method='post' action='$SELF_PHP'> 學號: <input type='text' name='student_no' size=9 value='$student_no'> <br> <input type='hidden' name='save_student_no' value='$student_no'> <input type='submit' name='command' value='更新'> <input type='reset' name='command' value='清除 '> </form> </center> </body> </html> "); } } if ($command=="更新") { $save_student_no=$_POST["save_student_no"]; // 準備MySQL UPDATE命令 $sqlstr="update stmd set student_no='$student_no' where student_no='$save_student_no'"; $result=mysql_query($sqlstr,$conn); $no_of_rows=mysql_affected_rows($conn); if ($no_of_rows==1) echo("<html><center>更新成功</center></html>"); else echo mysql_error(); } ?> | |