两个文件夹,一个班级cls,一个学生stu。
两个表,一个班级cls,一个学生stu。
每个文件夹里有7个php文件:主界面cls.php-------增add.php,insert.php-------删delete.php-------改edit.php,update.php-------数据库sql.php
班级主界面cls.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>班级信息</title>
</head>
<body>
<center>
<h1>班级信息|<a href="add.php">添加班级</a></h1>
<table width="600px" border="1px">
<tr>
<th>id</th>
<th>班级</th>
<th>修改</th>
<th>删除</th>
</tr>
<?php
include'sql.php';
$sql = "select * from cls";
$rst = mysql_query($sql);
while ($row=mysql_fetch_assoc($rst)) {
echo "<tr>";
echo "<td>$row[id]</td>";
echo "<td>$row[name]</td>";
echo "<td><a href='edit.php?id={$row[id]}&name={$row[name]}'>修改</a></td>";
echo "<td><a href='delete.php?id={$row[id]}'>删除</a></td>";
echo "</tr>";
}
mysql_close(); ?>
</table>
</center>
</body>
</html>
增add.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>添加班级</title>
</head>
<body>
<form action="insert.php" method="post">
<center>
<table>
<h1>添加班级</h1>
<tr>
<td>班级名</td>
<td><input type="text" name="classname"></td>
</tr>
<tr>
<td><input type="submit" value="提交"></td>
</tr>
</table>
</center>
</form>
</body>
</html>
增insert.php
<?php
$name = $_POST['classname'];
$sql = "INSERT INTO cls(name) VALUES('{$name}')";
//echo "$sql";
include'sql.php';
mysql_query($sql);
echo "<script>alert('添加成功')</script>";
echo "<script>location='cls.php'</script>"; ?>
删delete.php
<?php
$id = $_GET['id'];
include'sql.php';
$sql = "DELETE FROM cls WHERE id={$id}";
mysql_query($sql);
echo "$sql";
echo "<script>location='cls.php'</script>"; ?>
改edit.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>修改班级</title>
</head>
<body>
<?php
$id = $_GET['id'];
$classname = $_GET['name']; ?>
<form action="update.php" method="get">
<table border="1px">
<input type="hidden" name='id' value="<?php echo "$id"; ?>">
<tr>
<td>班级修改为</td>
<td>
<select name="newname" id="">
<?php
include'sql.php';
$sql = "select * from cls";
$rst = mysql_query($sql);
while ($row=mysql_fetch_assoc($rst)) {
if ($row[name]==$classname) {
echo "<option value='{$classname}' selected='selected'>{$classname}</option>";
}else{
echo "<option value='{$row[name]}'>{$row[name]}</option>";
}
}
mysql_close();
?>
</select>
</td>
</tr>
<tr>
<td><input type="submit" value="提交"></td>
</tr>
</table>
</form>
</body>
</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
<?php
mysql_connect('localhost','root','123');
mysql_select_db(myclass);
?>