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