两个文件夹,一个班级cls,一个学生stu。
两个表,一个班级cls,一个学生stu。
每个文件夹里有7个php文件:主界面cls.php-------增add.php,insert.php-------删delete.php-------改edit.php,update.php-------数据库sql.php
班级主界面cls.php
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>班级信息</title> 6 </head> 7 <body> 8 <center> 9 <h1>班级信息|<a href="add.php">添加班级</a></h1> 10 <table width="600px" border="1px"> 11 <tr> 12 <th>id</th> 13 <th>班级</th> 14 <th>修改</th> 15 <th>删除</th> 16 </tr> 17 <?php 18 include'sql.php'; 19 $sql = "select * from cls"; 20 $rst = mysql_query($sql); 21 while ($row=mysql_fetch_assoc($rst)) { 22 echo "<tr>"; 23 echo "<td>$row[id]</td>"; 24 echo "<td>$row[name]</td>"; 25 echo "<td><a href='edit.php?id={$row[id]}&name={$row[name]}'>修改</a></td>"; 26 echo "<td><a href='delete.php?id={$row[id]}'>删除</a></td>"; 27 echo "</tr>"; 28 } 29 mysql_close(); 30 31 ?> 32 </table> 33 </center> 34 </body> 35 </html>
增add.php
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>添加班级</title> 6 </head> 7 <body> 8 <form action="insert.php" method="post"> 9 <center> 10 <table> 11 <h1>添加班级</h1> 12 <tr> 13 <td>班级名</td> 14 <td><input type="text" name="classname"></td> 15 </tr> 16 <tr> 17 <td><input type="submit" value="提交"></td> 18 </tr> 19 </table> 20 </center> 21 </form> 22 </body> 23 </html>
增insert.php
1 <?php 2 $name = $_POST['classname']; 3 $sql = "INSERT INTO cls(name) VALUES('{$name}')"; 4 //echo "$sql"; 5 include'sql.php'; 6 mysql_query($sql); 7 echo "<script>alert('添加成功')</script>"; 8 echo "<script>location='cls.php'</script>"; 9 10 ?>
删delete.php
1 <?php 2 $id = $_GET['id']; 3 include'sql.php'; 4 $sql = "DELETE FROM cls WHERE id={$id}"; 5 mysql_query($sql); 6 echo "$sql"; 7 echo "<script>location='cls.php'</script>"; 8 9 ?>
改edit.php
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>修改班级</title> 6 </head> 7 <body> 8 <?php 9 $id = $_GET['id']; 10 $classname = $_GET['name']; 11 12 ?> 13 <form action="update.php" method="get"> 14 <table border="1px"> 15 <input type="hidden" name='id' value="<?php echo "$id"; ?>"> 16 <tr> 17 <td>班级修改为</td> 18 <td> 19 <select name="newname" id=""> 20 <?php 21 include'sql.php'; 22 $sql = "select * from cls"; 23 $rst = mysql_query($sql); 24 while ($row=mysql_fetch_assoc($rst)) { 25 if ($row[name]==$classname) { 26 echo "<option value='{$classname}' selected='selected'>{$classname}</option>"; 27 }else{ 28 echo "<option value='{$row[name]}'>{$row[name]}</option>"; 29 } 30 } 31 mysql_close(); 32 ?> 33 </select> 34 </td> 35 </tr> 36 <tr> 37 <td><input type="submit" value="提交"></td> 38 </tr> 39 </table> 40 </form> 41 </body> 42 </html>
改update.php
<?php include'sql.php'; $newname = $_GET['newname']; $id = $_GET['id']; $sql = "UPDATE cls SET name='{$newname}' WHERE id={$id}"; mysql_query($sql); mysql_close(); echo "<script>alert('修改成功! ');location='cls.php'</script>"; ?>
数据库sql.php
1 <?php 2 mysql_connect('localhost','root','123'); 3 mysql_select_db(myclass); 4 ?>