PDO案例:学生信息管理系统

PDO实现学生信息的增删改查

主页:index.php

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>学生信息管理</title>
    <link type="text/css" rel="stylesheet" href="css/style.css"/>
</head>
<body>
<div class="content">
    <?php require_once('menu.htm')?>
    <h3>浏览学生信息</h3>
    <table>
        <tr><th>id</th><th>班级</th><th>姓名</th><th>年龄</th><th>操作</th></tr>
        <?php
            //1.连接数据库
            try{
                $pdo=new PDO("mysql:host=localhost;dbname=myapp","root","");
            }catch(PDOException $e){
                die("数据库连接失败".$e->getMessage());
            }
            //2.执行SQL查询,并解析与遍历
            $sql="SELECT * FROM stu";
            foreach($pdo->query($sql) as $row){
                $id=$row['id'];
                $classId=$row['classId'];
                $name=$row['name'];
                $age=$row['age'];
                echo "<tr>
                        <td>$id</td>
                        <td>$classId</td>
                        <td>$name</td>
                        <td>$age</td>
                        <td>
                            <a href='javascript:doDel($id)'>删除</a>
                            <a href='edit.php?id=$id'>修改</a>
                        </td>
                      </tr>";
            }
        ?>
    </table>
</div>
<script>
    function doDel(id){
        if(confirm('确定要删除吗?')){
            window.location='action.php?action=del&id='+id;
        }
    }
</script>
</body>
</html>
共用菜单块:menu.htm

<h2>学生信息管理</h2>
<a href="index.php">浏览学生</a>
<a href="add.php">增加学生</a>
<hr/>
css文件:style.css

.content{
    width: 600px;
    margin: 0 auto;
}
table{
    border-collapse: collapse;
    width: 600px;
}
th,td{
    border:1px solid #ccccff;
    padding: 5px;
}
td{
    text-align: center;
}
显示效果:


增加学生信息的界面:add.php
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>学生信息管理</title>
    <link type="text/css" rel="stylesheet" href="css/style.css"/>
</head>
<body>
<div class="content">
    <?php require_once('menu.htm')?>
    <h3>增加学生信息</h3>
    <form action="action.php?action=add" method="post">
        <table>
            <tr>
                <td>班级</td>
                <td><input type="text" name="classId"/></td>
            </tr>
            <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="submit" value="增加"/>
                    <input type="reset" value="重置"/>
                </td>
            </tr>
        </table>
    </form>
</div>
</body>
</html>
显示效果:
修改学生信息的界面:edit.php
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>学生信息管理</title>
    <link type="text/css" rel="stylesheet" href="css/style.css"/>
</head>
<body>
<div class="content">
    <?php require_once('menu.htm');
        //1.连接数据库
        try{
            $pdo=new PDO("mysql:host=localhost;dbname=myapp","root","");
        }catch(PDOException $e){
            die("数据库连接失败".$e->getMessage());
        }
        //2.执行SQL语句,获取要修改的信息
        $sql="SELECT * FROM stu WHERE id=".$_GET['id'];
        $stmt=$pdo->query($sql);
        if($stmt->rowCount()>0){
            $stu=$stmt->fetch(PDO::FETCH_ASSOC);//解析数据
        }else{
            die("没有要修改的数据");
        }
    ?>
    <h3>修改学生信息</h3>
    <form action="action.php?action=edit" method="post">
        <input type="hidden" name="id" value="<?php echo $stu['id']; ?>">
        <table>
            <tr>
                <td>班级</td>
                <td><input type="text" name="classId" value="<?php echo $stu['classId']; ?>"/></td>
            </tr>
            <tr>
                <td>姓名</td>
                <td><input type="text" name="name" value="<?php echo $stu['name']; ?>"/></td>
            </tr>
            <tr>
                <td>年龄</td>
                <td><input type="text" name="age" value="<?php echo $stu['age']; ?>"/></td>
            </tr>
            <tr>
                <td></td>
                <td>
                    <input type="submit" value="修改"/>
                    <input type="reset" value="重置"/>
                </td>
            </tr>
        </table>
    </form>
</div>
</body>
</html>
显示效果:
处理增删改操作的文件:action.php
<?php
//1.连接数据库
try{
    $pdo=new PDO("mysql:host=localhost;dbname=myapp","root","");
}catch(PDOException $e){
    die("fail to connect mysql".$e->getMessage());
}
$id=$_POST['id'];
$classId=$_POST['classId'];
$name=$_POST['name'];
$age=$_POST['age'];
//2.通过action的值做相应操作
switch($_GET['action']){
    case "add"://增加操作
        $sql="INSERT INTO stu VALUES (null,'{$classId}','{$name}','{$age}')";
        $rw=$pdo->exec($sql);
        if($rw>0){
            echo "<script>alert('add success');window.location='index.php';</script>";
        }else{
            echo "<script>alert('add fail');window.history.back();</script>";
        }
    break;
    case "del"://删除操作
        $id=$_GET['id'];
        $sql="DELETE FROM stu WHERE id={$id}";
        $pdo->exec($sql);
        header("Location:index.php");
        break;
    case "edit"://修改操作
        $sql="UPDATE stu set name='{$name}',classId={$classId},age={$age} WHERE id={$id}";
        $rw=$pdo->exec($sql);
        if($rw>0){
            echo "<script>alert('update success');window.location='index.php';</script>";
        }else{
            echo "<script>alert('update fail');window.history.back();</script>";
        }
}

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值