PHP-2.資料庫小功能

來源:互聯網
上載者:User

標籤:center   語句   失敗   尋找   function   input   local   select   注釋   

  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     else103         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 ?>

 

PHP-2.資料庫小功能

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.