1<?PHP2 /*3 * PHP Database part of the function implementation4 */5 $KCNUM= @$_post[' Kcnum '];6 //Establish a database connection7 $conn=mysql_connect(' localhost ', ' root ', ' 419257511 ');8 //Specify a connected database9 mysql_select_db(' Test ',$conn);Ten //set the client character set One mysql_query("Set names ' gb2312 '",$conn); A //execute the SQL statement to query the course number and assign the result to $result - $RESULT=mysql_query("SELECT * from" where course number = '$KCNUM‘"); - //stores the result of the query as an array in the $row the $row=Mysql_fetch_array($RESULT); - //Print_f ($row); - if($KCNUM!=NULL&&!$row) { - Echo"<script>alert (' No course number! ') </script> "; + } -?> + A<body> at -<form name= "FR1" method= "POST" > -<p align= "center" > -Query by Course Number: <input type= "Numbers" name= "Kcnum" value= "" > <input -Type= "Submit" Name= "BT1" value= "Find" > -</p> in<form name= "FR2" method= "POST" > -<center> to<table border= "1" > +<tr bgcolor= "Blue" > -<td> Course Number:</td> the<td><input type= "number" name= "Kcnum" *Value= "<?php Echo$row[' Course number ']?> ></td> $</tr>Panax Notoginseng<tr bgcolor= "Blue" > -<td> Course Name:</td> the<td><input type= "text" Name= "Kcname" +Value= "<?php Echo$row[' Course name ']?> ></td> A</tr> the<tr bgcolor= "Blue" > +<td> Commencement Semester:</td> -<td><input type= "number" name= "Kcterm" $Value= "<?php Echo$row[' Commencement semester ']?> ></td> $</tr> -<tr bgcolor= "Blue" > -<td> hours:</td> the<td><input type= "text" Name= "Kctime" -Value= "<?php Echo$row[' UI ']?> ></td>Wuyi</tr> the<tr bgcolor= "Blue" > -<td> credits:</td> Wu<td><input type= "number" name= "Kccredit" -Value= "<?php Echo$row[' Credits ']?> ></td> About</tr> $<tr bgcolor= "Blue" > -<TD colspan= "2" align= "center" ><input type= "Submit" Name= "BT2" -Value= "Modify" > <input type= "Submit" Name= "BT2" value= "Add" > <input -Type= "Submit" Name= "BT2" value= "Delete" > A +</tr> the</table> -</center> $</form> the</form> the</body> the the - in the the<?PHP About the $KCnum= @$_post[' Kcnum ']; the $KCname= @$_post[' Kcname ']; the $KCterm= @$_post[' Kcterm ']; + $KCtime= @$_post[' Kctime ']; - $KCcredit= @$_post[' Kccredit ']; the //a custom function used to determine the logicBayi functionTest$KCnum,$KCname,$KCterm,$KCtime,$KCcredit) the { the if(!$KCnum) - Echo"<script>alert (' Course number cannot be empty ') </script>"; - ElseIf(!$KCname) the Echo"<script>alert (' Course name cannot be empty ') </script>"; the ElseIf($KCterm> 8 | |$KCterm< 1) the Echo"<script>alert (' note the commencement semester should be 1-8 ') </script>"; the ElseIf(!Is_numeric($KCtime)) - Echo"<script>alert (' hours must be numbers ') </script>"; the } the //modifying Modules the if(@$_post[' bt2 '] = = "Modify") {94 //SQL Modify Statement the $upd _query= "Update two set course name = '$KCname', Commencement semester = '$KCterm', hours = '$KCtime', credits = '$KCcredit' Where course number = '$KCnum‘"; the //executes the upstream statement and assigns a value to $upd_result the $upd _result=mysql_query($upd _query);98 //mysql_affected_rows: Returns the number of record rows affected by the previous MySQL operation. About //mysql_affected_rows is used here to verify that the modification was successful - if(mysql_affected_rows($conn)! = 0)101 Echo"<script>alert (' modified successfully ') </script>";102 Else103 Echo"<script>alert (' modification failed ') </script>";104 } the //The contents of the following modules are the same as above, do not comment106 //Add Module107 if(@$_post[' bt2 '] = = "Add") {108Test$KCnum,$KCname,$KCterm,$KCtime,$KCcredit);109 $s _sql= "Select course number from both where course number = '$KCnum‘"; the $s _result=mysql_query($s _sql);111 $s _row=Mysql_fetch_array($s _result); the if(!$s _row){113 $ins _sql= "INSERT into two (course number, course name, commencement semester, hours, credits) VALUES ('$KCnum‘,‘$KCname‘,‘$KCterm‘,‘$KCtime‘,‘$KCcredit‘)"; the $ins _result=mysql_query($ins _sql) or die(' <script>alert ("Add Failure") </script> '); the if(mysql_affected_rows($conn)! = 0) the Echo"<script>alert (' Add success! ') </script> ";117 Else 118 Echo"<script>alert (' Unknown error! ') </script> ";119 } - Else {121 Echo"<script>alert (' The course number already exists, cannot be added! ') </script> ";122 }123 }124 //Deleting a module the if(@$_post[' bt2 '] = = "Delete") {126 $d _sql= "Select course number from both where course number = '$KCnum‘";127 $d _result=mysql_query($d _sql); - $d _row=Mysql_fetch_array($d _result);129 if(!$d _row) the Echo"<script>alert (' course number does not exist ') </script>";131 Else{ the $del _sql= "Delete from both where course number = '$KCnum‘";133 $del _result=mysql_query($del _sql) or die(' Delete failed ');134 if(mysql_affected_rows($conn)!=0)135 Echo"<script>alert (' deletion succeeded ') </script>";136 }137 }138?>
PHP-2. Database Small function