羅德興老師的教學歷程檔案 - 112-2 資料庫管理系統 (DBMS) - PHP 與 資料庫 (一) |
|
|
PHP 與 資料庫 (一)
PHP 與 資料庫 <!-- form1-1.html --> <!-- HTML表單 form --> <html> <!-- HTML 開始 --> <body> <!-- 本體 --> <center> <!-- 置中 --> <form method=post action=form1-2.php> <!-- 表單開始 form --> <!-- post 表示不顯示在url --> <!-- get 表示顯示在url --> <!-- action 表要呼叫的程式 --> 請輸入一個整數: <input type=text name="n" size=10> <!-- type=text 字輸入框 --> <!-- name=n 變數名稱為 n --> <!-- size=10 可輸入10個字元 --> <p> <input type=submit name=command value=送出> <!-- 命令按鈕 顯示送出 --> <input type=reset name=command value=清除> <!-- 清除按鈕 顯示清除 --> </form> <!-- 表單結束 --> </center> <!-- 置中結束 --> </body> <!-- 本體結束 --> </html> <!-- HTML結束 -->
<!-- form1-2.php --> <!-- 取出form變數內容 --> <html> <!-- HTML 開始 --> <body> <!-- 本體 --> <center> <!-- 置中 --> <?php // php程式 $n=$_POST["n"]; // 取出變數值 echo "您輸入的字串為" . $n . "<br>"; // 輸出結果 ?> </center> <!-- 置中結束 --> </body> <!-- 本體結束 --> </html> <!-- HTML結束 -->
<!-- form2.php --> <!-- PHP程式架構 --> <?php // 結合兩個程式為一個,並完全以PHP撰寫 $command=$_POST['command']; // 取出變數值 if ($command==null) { // 空值, 未經由form傳送 echo "<html>"; echo "<body>"; echo "<center>"; echo "<form method=post action=$SELF_PHP>"; echo "請輸入一個字串:"; echo "<input type=text name=n size=10>"; echo "<p>"; echo "<input type=submit name=command value=送出>"; echo "<input type=reset name=command value=清除>"; echo "</form>"; echo "</center>"; echo "</body>"; echo "</html>"; } else if ($command=='送出') { $n=$_POST["n"]; echo "<html>"; echo "<body>"; echo "<center>"; echo "您輸入的字串為" . $n . "<br>"; echo "</center>"; echo "</body>"; echo "</html>"; } ?> <!-- pass.php --> <!-- Type=password --> <?php $command=$_POST['command']; // 取出變數值 if ($command==null) { // 空值, 未經由form傳送 echo "<html>"; echo "<body>"; echo "<center>"; echo "<form method=post action=$SELF_PHP>"; echo "使用者名稱:"; echo "<input type=text name=username size=10>"; echo "<p>"; echo "使用者密碼:"; echo "<input type=password name=password size=10>"; echo "<p>"; echo "<input type=submit name=command value=送出>"; echo "<input type=reset name=command value=清除>"; echo "</form>"; echo "</center>"; echo "</body>"; echo "</html>"; } else if ($command=='送出') { $username=$_POST["username"]; $password=$_POST["password"]; echo "<html>"; echo "<body>"; echo "<center>"; echo "使用者名稱為" . $username . "<br>"; echo "使用者密碼為" . $password . "<br>"; echo "</center>"; echo "</body>"; echo "</html>"; } ?>
<!-- ex-hidden.php --> <!-- Type=hidden --> <?php $command=$_POST['command']; // 取出變數值 if ($command==null) { // 空值, 未經由form傳送 echo "<html>"; echo "<body>"; echo "<center>"; echo "<form method=post action=$SELF_PHP>"; echo "使用者名稱:"; echo "<input type=text name=username size=10>"; echo "<p>"; echo "使用者密碼:"; echo "<input type=password name=password size=10>"; echo "<p>"; echo "<input type=hidden name=number value='This is a hidden value'>"; echo "<input type=submit name=command value=送出>"; echo "<input type=reset name=command value=清除>"; echo "</form>"; echo "</center>"; echo "</body>"; echo "</html>"; } else if ($command=='送出') { $username=$_POST["username"]; $password=$_POST["password"]; $hidden_value=$_POST['number']; echo "<html>"; echo "<body>"; echo "<center>"; echo "使用者名稱為" . $username . "<br>"; echo "使用者密碼為" . $password . "<br>"; echo "隱藏值為" . $hidden_value . "<br>"; echo "</center>"; echo "</body>"; echo "</html>"; } ?>
<!-- ex-textarea.php --> <!-- Textarea --> <?php $command=$_POST["command"]; if ($command==null) { echo("<html> <body> <center> <form method=post action=$SELF_PHP> 備註: <p> <textarea name='note' rows=5 cols=40> </textarea> <p> <input type=submit name=command value=送出> <input type=reset name=command value=清除> </form> </center> </body> </html>"); } if ($command=="送出") { $note=$_POST["note"]; echo("<html> <body> <center> 備註:$note <p> </center> </body> </html>"); } ?>
<!-- ex-radio.php --> <!-- Type=radio --> <?php $command=$_POST["command"]; if ($command==null) { echo("<html> <body> <center> <form method=post action='$SELF_PHP'> <input type=radio name=choice value=1>選項 A <br> <input type=radio name=choice value=2>選項 B <br> <input type=radio name=choice value=3>選項 C <br> <input type=submit name=command value=送出> <input type=reset name=command value=清除> </form> </center> </body> </html>"); } if ($command=="送出") { $choice=$_POST["choice"]; echo("<html> <body> <center> 您的選項是: $choice <p> </center> </body> </html>"); } ?> <!-- ex-radio2.php --> <!-- Type=Radio 的應用範例 --> <?php $command=$_POST["command"]; if ($command==null) { echo("<html> <body> <center> <form method=post action=$SELF_PHP> <p> 數目1 <input type=text name=num1 size=5> <input type=radio name=choice value='+'>+ <input type=radio name=choice value='-'>- <input type=radio name=choice value='*'>* <input type=radio name=choice value='/'>/ 數目2 <input type=text name=num2 size=5><p> <input type=submit name=command value=送出> <input type=reset name=command value=清除> </form> </center> </body> </html>"); } if ($command=="送出") { $num1=$_POST["num1"]; $choice=$_POST["choice"]; $num2=$_POST["num2"]; switch ($choice) { case "+" : $result=$num1+$num2; $plus ="checked"; break; case "-" : $result=$num1-$num2; $minus="checked"; break; case "*" : $result=$num1*$num2; $times="checked"; break; case "/" : $result=$num1/$num2; $div ="checked"; break; } echo("<html> <body> <center> 計算結果= $result </center> </body> </html>"); } ?> <!-- ex-checkbox2.php --> <!-- Type=checkbox之一 --> <?php $command=$_POST["command"]; if ($command==null) { echo("<html> <body> <center> <form method=post action=$SELF_PHP> <input type=checkbox name=choice1 value=1>選項 A <br> <input type=checkbox name=choice2 value=2>選項 B <br> <input type=checkbox name=choice3 value=3>選項 C <br> <input type=submit name=command value=送出> <input type=reset name=command value=清除> </form> </center> </body> </html>"); } if ($command=="送出") { $choice1=$_POST["choice1"]; $choice2=$_POST["choice2"]; $choice3=$_POST["choice3"]; $bk = ' '; $result=$choice1 . $bk . $choice2 . $bk . $choice3; echo("<html> <body> <center> 您的選項是: $result <p> </center> </body> </html>"); } ?>
<!-- ex-checkbox3.php --> <!-- Type=checkbox之二 & Array --> <?php $command=$_POST["command"]; if ($command==null) { echo("<html> <body> <center> <form method=post action=$SELF_PHP> 選項: <p> <input type=checkbox name=choice[] value=1>choice 1 <p> <input type=checkbox name=choice[] value=2>choice 2 <p> <input type=checkbox name=choice[] value=3>choice 3 <p> <input type=checkbox name=choice[] value=4>choice 4 <p> <input type=checkbox name=choice[] value=5>choice 5 <p> <input type=submit name=command value=送出> <input type=reset name=command value=清除> </form> </center> </body> </html>"); } if ($command=="送出") { $choice=$_POST["choice"]; $ca = ','; $result=$choice[0].$ca.$choice[1].$ca.$choice[2].$ca.$choice[3].$ca.$choice[4]; echo("<html> <body> <center> 選項:$result; <p> </center> </body> </html>"); } ?>
<!-- ex-checkbox4.php --> <!-- Type=checkbox之三 & Loop --> <?php $command=$_POST["command"]; $ca = ','; if ($command==null) { echo("<html> <body> <center> <form method=post action=$SELF_PHP> 選項: <p> <input type=checkbox name=choice[] value=1>choice 1 <p> <input type=checkbox name=choice[] value=2>choice 2 <p> <input type=checkbox name=choice[] value=3>choice 3 <p> <input type=checkbox name=choice[] value=4>choice 4 <p> <input type=checkbox name=choice[] value=5>choice 5 <p> <input type=submit name=command value=送出> <input type=reset name=command value=清除> </form> </center> </body> </html>"); } if ($command=="送出") { $choice=$_POST["choice"]; foreach ($choice as $number) $result=$result.$ca.$number; echo("<html> <body> <center> 您的選項是: $result <p> </center> </body> </html>"); } ?>
<!-- ex-select2.php --> <!-- Select之一 --> <?php $command=$_POST["command"]; if ($command==null) { echo("<html> <body> <center> <form method=post action=$SELF_PHP> 選項: <p> <select name=choice size=1> <option value=1>choice 1 </option> <option value=2>choice 2 </option> <option value=3 selected>choice 3 </option> </select> <p> <input type=submit name=command value=送出> <input type=reset name=command value=清除> </form> </center> </body> </html>"); } if ($command=="送出") { $choice=$_POST["choice"]; echo("<html> <body> <center> 選項: $choice <p> </center> </body> </html>"); } ?>
<!-- ex-select3.php --> <!-- Select之二 --> <?php $command=$_POST["command"]; if ($command==null) { echo("<html> <body> <center> <form method=post action=$SELF_PHP> <input type=text name=num1 size=5> <select name=choice size=1> <option value=+>+ </option> <option value=->- </option> <option value=*>* </option> <option value=/>/ </option> </select> <input type=text name=num2 size=5> = <input type=text name=result size=5> <p> <input type=submit name=command value=送出> <input type=reset name=command value=清除> </form> </center> </body> </html>"); } if ($command=="送出") { $choice=$_POST["choice"]; echo("<html> <body> <center> 選項:$choice <p> </center> </body> </html>"); } ?>
<!-- ex-select4.php --> <!-- Select之三 --> <?php $command=$_POST["command"]; if ($command==null) { echo("<html> <body> <center> <form method=post action=$SELF_PHP> 選項: <p> <input type=checkbox name=choice[] value=1>choice 1 <p> <input type=checkbox name=choice[] value=2>choice 2 <p> <input type=checkbox name=choice[] value=3>choice 3 <p> <input type=checkbox name=choice[] value=4>choice 4 <p> <input type=checkbox name=choice[] value=5>choice 5 <p> <input type=submit name=command value=送出> <input type=reset name=command value=清除> </form> </center> </body> </html>"); } if ($command=="送出") { $choice=$_POST["choice"]; foreach ($choice as $number) $result=$result.$number; echo("<html> <body> <center> 選項:$result <p> </center> </body> </html>"); } ?>
<!-- ex-file.php --> <!-- File上傳 --> <?php $command=$_POST["command"]; if ($command==null) { echo("<html> <body> <center> <form method=post enctype=multipart/form-data action=$SELF_PHP> 請選取檔案: <input type=file name=filename> <input type=submit name=command value=送出> <input type=reset name=command value=清除> </form> </center> </body> </html>"); } if ($command=="送出") { $filename=$_FILES["filename"]["name"]; $tempname=$_FILES["filename"]["tmp_name"]; unlink($filename); if(copy($tempname,$filename)==1) echo "檔案上傳 O.K. "; } ?>
|
|
中華科技大學數位化學習歷程 - 意見反應 |