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