PHP-2.數據庫小功能

  1 <?php
  2 /*
  3  * 《PHP數據庫部分功能實現》
  4  */
  5 $KCNUM = @$_POST['KCNUM'];
  6 //創建一個數據庫鏈接
  7 $conn = mysql_connect('localhost', 'root', '419257511');
  8 //指定鏈接的數據庫
  9 mysql_select_db('test', $conn);
 10 //設置客戶端字符集
 11 mysql_query("set names 'gb2312'", $conn);
 12 //執行sql語句查詢課程號並將結果賦值給$RESULT
 13 $RESULT = mysql_query("select * from two where 課程號='$KCNUM'");
 14 //將查詢到的結果做爲數組存儲在$row
 15 $row = mysql_fetch_array($RESULT);
 16 //print_f($row);
 17 if ($KCNUM != null && ! $row) {
 18     echo "<script>alert('沒有課程號!')</script>";
 19 }
 20 ?>
 21 <html>
 22 <body>
 23     <h3 align="center">課程表操做</h3>
 24     <form name="fr1" method="post">
 25         <p align="center">
 26             根據課程號查詢:<input type="number" name="KCNUM" value=""> <input
 27                 type="submit" name="bt1" value="查找">
 28         </p>
 29         <form name="fr2" method="post">
 30             <center>
 31                 <table border="1">
 32                     <tr bgcolor="blue">
 33                         <td>課程號:</td>
 34                         <td><input type="number" name="KCnum"
 35                             value="<?php echo $row['課程號'] ?>"></td>
 36                     </tr>
 37                     <tr bgcolor="blue">
 38                         <td>課程名:</td>
 39                         <td><input type="text" name="KCname"
 40                             value="<?php echo $row['課程名'] ?>"></td>
 41                     </tr>
 42                     <tr bgcolor="blue">
 43                         <td>開課學期:</td>
 44                         <td><input type="number" name="KCterm"
 45                             value="<?php echo $row['開課學期'] ?>"></td>
 46                     </tr>
 47                     <tr bgcolor="blue">
 48                         <td>學時:</td>
 49                         <td><input type="text" name="KCtime"
 50                             value="<?php echo $row['學時'] ?>"></td>
 51                     </tr>
 52                     <tr bgcolor="blue">
 53                         <td>學分:</td>
 54                         <td><input type="number" name="KCcredit"
 55                             value="<?php echo $row['學分'] ?>"></td>
 56                     </tr>
 57                     <tr bgcolor="blue">
 58                         <td colspan="2" align="center"><input type="submit" name="bt2"
 59                             value="修改"> <input type="submit" name="bt2" value="添加"> <input
 60                             type="submit" name="bt2" value="刪除">
 61                     
 62                     </tr>
 63                 </table>
 64             </center>
 65         </form>
 66     </form>
 67 </body>
 68 </html>
 69 
 70 
 71 
 72 
 73 <?php
 74 
 75 $KCnum = @$_POST['KCnum'];
 76 $KCname = @$_POST['KCname'];
 77 $KCterm = @$_POST['KCterm'];
 78 $KCtime = @$_POST['KCtime'];
 79 $KCcredit = @$_POST['KCcredit'];
 80 //自定義函數,用來判斷邏輯
 81 function test($KCnum, $KCname, $KCterm, $KCtime, $KCcredit)
 82 {
 83     if (!$KCnum)
 84         echo "<script>alert('課程號不能爲空')</script>";
 85     elseif (!$KCname)
 86         echo "<script>alert('課程名不能爲空')</script>";
 87     elseif ($KCterm > 8 || $KCterm < 1)
 88         echo "<script>alert('注意開課學期應爲1-8')</script>";
 89     elseif (!is_numeric($KCtime))
 90         echo "<script>alert('學時必須是數字')</script>";
 91 }
 92 //修改模塊
 93 if (@$_POST['bt2'] == "修改") {
 94     //sql修改語句
 95     $upd_query = "update two set 課程名='$KCname',開課學期='$KCterm',學時='$KCtime',學分='$KCcredit'where 課程號='$KCnum'";
 96     //執行上行語句並賦值給$upd_result
 97     $upd_result = mysql_query($upd_query);
 98     //mysql_affected_rows:返回前一次 MySQL 操做所影響的記錄行數。
 99     //mysql_affected_rows 在此處用來驗證是否修改爲功
100     if (mysql_affected_rows($conn) != 0)
101         echo "<script>alert('修改爲功')</script>";
102     else
103         echo "<script>alert('修改失敗')</script>";
104 }
105 //如下模塊內容同上,就不作註釋了
106 //添加模塊
107 if (@$_POST['bt2'] == "添加") {
108     test($KCnum, $KCname, $KCterm, $KCtime, $KCcredit);
109     $s_sql = "select 課程號 from two where 課程號='$KCnum'";
110     $s_result = mysql_query($s_sql);
111     $s_row = mysql_fetch_array($s_result);
112     if (!$s_row){
113         $ins_sql = "insert into two (課程號,課程名,開課學期,學時,學分) values('$KCnum','$KCname','$KCterm','$KCtime','$KCcredit')";
114         $ins_result = mysql_query($ins_sql) or die('<script>alert("添加失敗")</script>');
115         if (mysql_affected_rows($conn) != 0)
116             echo "<script>alert('添加成功!')</script>";
117         else 
118             echo "<script>alert('未知錯誤!')</script>";
119     }
120     else {
121         echo "<script>alert('該課程號已存在,沒法添加!')</script>";
122     }
123 }
124 //刪除模塊
125 if (@$_POST['bt2'] == "刪除") {
126     $d_sql = "select 課程號 from two where 課程號='$KCnum'";
127     $d_result=mysql_query($d_sql);
128     $d_row=mysql_fetch_array($d_result);
129     if(!$d_row)
130         echo "<script>alert('課程號不存在')</script>";
131        else{
132             $del_sql="delete from two where 課程號='$KCnum'";
133             $del_result=mysql_query($del_sql) or die('刪除失敗');
134             if(mysql_affected_rows($conn)!=0)
135                 echo "<script>alert('刪除成功')</script>";
136         }
137 }
138 ?>
相關文章
相關標籤/搜索