数据库---学生管理系统
大致步骤:
1.新建一个数据库文件(eg:xxx.sql)
页面--“studata.sql”(数据库文件)
-- 以下sql脚本可以在 编辑器中执行 -- 选择数据库 use newsdb; -- 创建数据表 create table stu ( stuid int auto_increment primary key comment '学生编号', name varchar(20) not null comment '姓名', roomno varchar(20) not null comment '宿舍号', addtime timestamp default current_timestamp not null comment '发布时间' )charset=utf8; -- 插入测试数据 insert into stu (name, roomno) values ('张三', '101'), ('李四', '101'), ('哈尼克孜', '201'); -- 查看测试数据 *全部列 select * from stu
2.将设计数据库、数据表的代码放到SQL编辑器中,然后运行,执行SQL脚本

3.db.php 连接数据库的公共php文件 提供复用(固定代码)
页面--“db.php”
<meta charset="utf-8"> <?php //设置字符集 header('Content-Type:text/html;charset=utf-8'); //设置数据库的DSN信息 $dsn = 'mysql:host=localhost;dbname=newsdb;charset=utf8'; try{ //$pdo数据库对象 $pdo = new PDO($dsn, 'root', 'root'); }catch(PDOException $e){ //连接失败,输出异常信息 exit('PDO连接数据库失败:'.$e->getMessage()); } ?>
4.拼写sql 查询 语句 pdo执行sql语句
页面--“index.php”(默认首页)
<?php require "db.php"; $sql = "select stuid,name,roomno,addtime from stu"; $res = $pdo->query($sql); $stus = $res->fetchAll(PDO::FETCH_ASSOC); var_dump($stus); require "stutable.html"; ?>
5.添加语句(insert into)
页面--“add.php”
<?php require "db.php"; if($_POST){ echo "是post 请求"; $sn = $_POST['stuname']; $rn = $_POST['room']; $sql = "insert into stu (name,roomno) values('$sn','$rn')"; //var_dump($sql); $pdo->query($sql); unset($pdo); header("Location: index.php"); } else{ echo "get 请求"; require "add.html"; } ?>
页面--“add.html”
<!doctype html> <html> <head> <meta charset="utf-8"> <title>学生管理系统</title> <link rel="stylesheet" href="./css/style.css" /> </head> <body> <div class="box"> <div class="top"> <div class="title">学生管理系统</div> <div class="nav"> <a href="./index.php">返回列表</a> </div> </div> <div class="main"> <form method="post"> <table class="news-edit"> <tr> <th>姓名:</th> <td><input type="text" name="stuname" placeholder="填写学生姓名" /></td> </tr> <tr> <th>宿舍:</th> <td> <select name="room"> <option>101</option> <option>102</option> <option>103</option> </select> </td> </tr> <tr> <th></th> <td><input type="submit" value="提交信息" /></td> </tr> </table> </form> </div> </div> </body> </html>
6.删除语句(delete)
页面--“del.php”
<?php require "db.php"; $stuid = $_GET["stuid"]; $sql = "delete from stu where stuid = $stuid "; $res = $pdo->query($sql); //sql语句正确 返回obj 否则 返回false var_dump($res); unset($pdo);//unset将对象设置为null header('Location:index.php'); ?>
7.修改语句(update)
页面--“up.php”
<?php require "db.php"; $stuid = $_GET["stuid"]; if($_POST){ echo "用户点击了修改"; $sn = $_POST["stuname"]; $rn = $_POST["room"]; echo "$sn$rn"; $sql = "update stu set name='$sn',roomno='$rn' where stuid=$stuid"; $pdo->query($sql); header('Location:index.php'); } $sql = "select * from stu where stuid=$stuid "; $res = $pdo->query($sql); //fetch获取 一个数据 $stu = $res->fetch(PDO::FETCH_ASSOC); unset($pdo); require "up.html"; ?>
页面--“up.html”
<!doctype html> <html> <head> <meta charset="utf-8"> <title>学生管理系统</title> <link rel="stylesheet" href="./css/style.css" /> </head> <body> <div class="box"> <div class="top"> <div class="title">学生管理系统</div> <div class="nav"> <a href="./index.php">返回列表</a> </div> </div> <div class="main"> <form method="post"> <table class="news-edit"> <tr> <th>姓名:</th> <td><input type="text" name="stuname" value="<?php echo $stu['name'];?>" /></td> </tr> <tr> <th>宿舍:</th> <td>
<?php echo $stu['roomno'];?> <select name="room"> <option <?php echo $stu['roomno']=="101"?"selected":"";?>>101</option>//用三元运算符使对应“宿舍号”被选中 <option <?php echo $stu['roomno']=="102"?"selected":"";?>>102</option> <option <?php echo $stu['roomno']=="103"?"selected":"";?>>103</option> </select> </td> </tr> <tr> <th></th> <td><input type="submit" value="提交信息" /></td> </tr> </table> </form> </div> </div> </body> </html>

浙公网安备 33010602011771号