| 以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=="新增") { $depart_class=$_POST["depart_class"]; $student_no=$_POST["student_no"]; $student_name=$_POST["student_name"]; $birth_date=$_POST["birth_date"]; if ($student_no==null) { echo "<center>學號欄不能空白,請回前頁重新輸入</center><p>"; exit(); } $sql="insert into stmd values('$depart_class','$student_no','$student_name','$birth_date')"; $result=mysql_query($sql,$conn); $no_of_rows=mysql_affected_rows($conn); if ($no_of_rows==1) echo("<html><center>新增成功</center></html>"); else echo mysql_error(); } if ($command=="查詢") { $student_no=$_POST["student_no"]; if ($student_no==null || $student_no=="") $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>0) { echo("<html><body><center>"); while ($myrow=mysql_fetch_array($result)) { $student_no=$myrow["student_no"]; echo $student_no ."<br>"; } echo("</center></body></html>"); } } ?> | |