menu.php
<html>
<head>
<meta charset="UTF-8">
</head>
<h2>学生信息管理</h2>
<a href="index.php">浏览学生</a>
<a href="add.php">增加学生</a>
<hr/>
</html>
index.php
<html>
<head>
<meta charset="UTF-8">
<title>学生信息管理</title>
<script>
function doDel(id)
{
if(confirm("确定要删除吗?")){
window.location = 'action.php?action=del&id='+id;
}
}
</script>
</head>
<body>
<center>
<?php
include("menu.php");
?>
<h3>浏览学生信息</h3>
<table width = "600" border = "1" >
<tr>
<th>ID</th>
<th>姓名</th>
<th>年龄</th>
<th>操作</th>
</tr>
<?php
try{
#连接数据库
$pdo = new PDO("mysql:host=localhost;dbname=php","root","root");
}catch(PDOException $e){
die("数据库连接失败".$e->getMessage());
}
#print_r($pdo);
##执行sql语句
$sql = "select * from test";
foreach($pdo->query($sql) as $row)
{
echo "<tr>";
echo "<td>{$row['id']}</td>";
echo "<td>{$row['name']}</td>";
echo "<td>{$row['age']}</td>";
echo "<td>{$row['sex']}</td>";
echo "<td>
<a href='javascript:doDel({$row['id']})'>删除</a>
<a href='edit.php?id={$row['id']}'>修改</a>
</td>";
echo "</tr>";
}
?>
</table>
</center>
</body>
</html>
edit.php
<!DOCTYPE html>
<html>
<head>
<meta charset = "UTF-8">
<title>修改学生信息</title>
</head>
<body>
<center>
<?php
include("menu.php");
#连接数据库
try{
$pdo = new PDO("mysql:host=localhost;dbname=php","root","root");
}catch(PDOException $e){
die("数据库连接失败".$e->getMessage());
}
#拼成sql语句,查询信息
$sql = "select * from test where id = ".$_GET['id'];
$stmt = $pdo->query($sql);
#var_dump($stmt);
if($stmt->rowCount() > 0)
{
$arr = $stmt->fetch(PDO::FETCH_ASSOC);
# die("没有需要修改的数据");
# var_dump($arr);
}
else
{
die("没有需要修改的数据");
}
?>
<form action="action.php?action=edit" method = "post">
<input type="hidden" name="id" value="<?php echo $arr['id']; ?>" />
<table>
<tr>
<td>姓名</td>
<td><input type = "text" name = "name" value="<?php echo $arr['name'];?>" ></td>
<td></td>
</tr>
<tr>
<td>年龄</td>
<td><input type = "text" name = "age" value= <?php echo $arr['age'];?>></td>
</tr>
<tr>
<td>姓名</td>
<td><input type = "radio" name = "sex" value = '男' <?php echo ($arr['sex']) == '男' ? "checked": "" ?> > 男
<input type = "radio" name = "sex" value = '女' <?php echo ($arr['sex']) == '女' ? "checked" : ""?>> 女</td>
<td></td>
</tr>
<tr>
<td> </td>
<td><input type = "submit" value = "修改"/>
<input type = "reset" value = "重置"/>
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
add.php
<html>
<head>
<title>学生管理信息</title>
</head>
<body>
<center>
<?php
include("menu.php");
?>
<h3>增加学生信息</h3>
<form action="action.php?action=add" method = "post">
<table>
<tr>
<td>姓名</td>
<td>
<input type = "text" name="name" /></td>
</tr>
<tr>
<td>年龄</td>
<td>
<input type = "text" name="age" />
</td>
</tr>
<tr>
<td>性别</td>
<td>
<input type = "radio" name="sex" value = "男" /> 男
<input type = "radio" name="sex" value = "女" /> 女
</td>
</tr>
<tr>
<td> </td>
<td>
<input type = "submit" value = "增加" />
<input type = "reset" value = "重置" />
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
action.php
<html>
<head>
<meta charset=“UTF-8">
</head>
<body>
<?php
#1.连接数据库
try{
$pdo = new PDO("mysql:host=localhost;dbname=php","root","root");
}catch(PDOException $e){
die("数据库连接失败".$e->getMessage());
}
#2.通过aaction的值判断相应的操作
switch($_GET['action'])
{
case 'add'://添加的操作
$name = $_POST['name'];
$age = $_POST['age'];
$sex = $_POST['sex'];
$sql = "insert into test value(null,'{$name}','{$age}','{$sex}')";
$rw = $pdo->exec($sql);
if($rw > 0)
{
echo "<script>alert('SUCCESS');window.location='index.php'</script>";
}
else
{
echo "<script>alert('FAIL');window.history.back();</script>";
}
break;
case 'del':
$id = $_GET['id'];
$sql = "delete from test where id = {$id}";
$pdo->exec($sql);
header("location:index.php");
break;
case 'edit':
$id = $_POST['id'];
$name = $_POST['name'];
$age = $_POST['age'];
$sex = $_POST['sex'];
$sql = "update test set name = '{$name}',age = {$age},sex = '{$sex}' where id = {$id}";
$res = $pdo->exec($sql);
#echo $res;
if($res > 0)
{
echo "<script>alert('SUCCESS');window.location='index.php'</script>";
}
else
{
echo "<script>alert('FAIL');window.history.back();</script>";
}
break;
}
?>
</body>
</html>