数据库:宿舍表和学生表——主键与外键联系
第一步:建一个文件夹——1105
第二步:建一个后缀为.sql的文件——studata.sql,在其文件中设计创建数据库和数据表的代码并在SQL编辑器中运行。

1 -- 以下sql脚本可以在 编辑器中执行 2 create database db1105; 3 -- 选择数据库 4 use db1105; 5 -- 宿舍表 6 create table room ( 7 rid int auto_increment primary key comment '宿舍主键', 8 rnum varchar(10) not null comment '宿舍号', 9 rcount int not null comment '人数', 10 addtime timestamp default current_timestamp not null comment '发布时间' 11 )charset=utf8; 12 13 insert into room (rnum,rcount) values 14 ('204', 8), 15 ('205', 8), 16 ('206', 6); 17 18 -- 学生表 19 create table stu ( 20 stuid int auto_increment primary key comment '学生编号', 21 name varchar(20) not null comment '姓名', 22 rid int not null comment '宿舍表外键', 23 addtime timestamp default current_timestamp not null comment '发布时间' 24 )charset=utf8; 25 26 -- 插入测试数据 27 insert into stu (name, rid) values 28 ('张三', 1), 29 ('李四', 1), 30 ('哈尼克孜', 4); 31 -- 连接 联表查询 join 平衡 left偏左 32 select stu.name,room.rnum 33 from stu join room 34 on stu.rid = room.rid
第三步:建一个页面——db.php,里面编写连接数据库的代码(固定代码)并在浏览器中运行。

1 <meta charset="utf-8"> 2 <?php 3 //设置字符集 4 header('Content-Type:text/html;charset=utf-8'); 5 //设置数据库的DSN信息 6 $dsn = 'mysql:host=localhost;dbname=db1105;charset=utf8'; 7 try{ 8 //$pdo数据库对象 9 $pdo = new PDO($dsn, 'root', 'root'); 10 }catch(PDOException $e){ 11 //连接失败,输出异常信息 12 exit('PDO连接数据库失败:'.$e->getMessage()); 13 }
第四步:在文件夹1105中新建两个文件夹——room、stu。
第五步:分别在文件夹room、stu中编写实现增、删、查、改功能的代码。具体如下:
room中的增、删、查、改:

1 <?php 2 require "../db.php"; 3 $sql = "select rid,rnum,rcount,addtime from room"; 4 $res = $pdo->query($sql);//$res查询结果集 5 //var_dump($res); 6 //fetchAll 结果集 转换 数组-数组 多个 7 $room = $res->fetchAll(PDO::FETCH_ASSOC); 8 unset($pdo); 9 require "index.html"; 10 ?>

1 <table border="1"> 2 <tr><td>宿舍</td><td>人数</td><td>删除</td><td>修改</td></tr> 3 <?php foreach($room as $r):?> 4 <tr> 5 <td><?php echo $r["rnum"];?></td> 6 <td><?php echo $r["rcount"];?></td> 7 <td><a href="del.php?rid=<?php echo $r['rid'];?>">删除</a></td> 8 <td><a href="up.php?rid=<?php echo $r['rid'];?>">修改</a></td> 9 </tr> 10 <?php endforeach;?> 11 </table>

1 <?php 2 require "../db.php"; 3 if($_POST){ 4 echo "添加宿舍"; 5 $no = $_POST["rnum"]; 6 $ct = $_POST["rcount"]; 7 $sql = "insert into room(rnum,rcount) values('$no',$ct)"; 8 var_dump($sql); 9 $r = $pdo->query($sql); 10 var_dump($r); 11 header('Location:index.php'); 12 } 13 else{ 14 echo "get"; 15 $sql = "select * from room"; 16 $res = $pdo->query($sql); 17 $room = $res->fetchAll(PDO::FETCH_ASSOC); 18 require "add.html"; 19 } 20 ?>

1 <script type="text/javascript" src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script> 2 3 <script type="text/javascript"> 4 $(document).ready(function(){ //页面加载完成后 5 $("#fr").submit(function(){ //表单提交事件 6 if($("#rnum").val()==""){ 7 $("#rnum").css("border-color","red"); 8 return false; 9 } 10 11 }); 12 }); 13 14 </script> 15 16 <form method="post" id = "fr"> 17 宿舍号<input type="text" name="rnum" id="rnum" value="<?php echo $r['rnum'];?>" /><br/> 18 人数<input type="number" name="rcount"> 19 <input type="submit" name=""> 20 </form>

1 <?php 2 require "../db.php"; 3 $rid = $_GET["rid"]; 4 //select * from stu where rid = 7 5 $sql = "select * from stu where rid=$rid"; 6 $rstu=$pdo->query($sql); 7 $rstu->rowCount();//查询结果集的个数 8 if($rstu->rowCount()>0){ 9 echo "无法删除"; 10 } 11 else{ 12 $sql = "delete from room where rid=$rid"; 13 $pdo->query($sql); 14 header('Location:index.php'); 15 } 16 ?>
stu中的增、删、查、改:

1 <?php 2 require "../db.php"; 3 $sql = "select stuid,sname,rnum from stu join room on stu.rid = room.rid"; 4 $res = $pdo->query($sql);//结果集 5 if($res){ 6 $rstus = $res->fetchAll(PDO::FETCH_ASSOC);//转换数组 7 var_dump($rstus); 8 } 9 else{ 10 echo "查询有误"; 11 } 12 ?> 13 <h5>学生表</h5> 14 <a href="all.php">批量修改宿舍</a> 15 <table border="1"> 16 <tr><th>姓名</th><th>宿舍</th><th>修改</th></tr> 17 <?php foreach ($rstus as $s) {?> 18 <tr><td><?php echo $s["sname"]; ?></td> 19 <td><?php echo $s["rnum"]; ?></td> 20 <td><a href="up.php?sid=<?php echo $s["stuid"]; ?>">修改</a></td> 21 </tr> 22 <?php } ?> 23 </table>

1 <?php 2 require "../db.php"; 3 if($_POST){ 4 $rid = $_POST["rid"]; 5 $sname = $_POST["sname"]; 6 $sql = "insert into stu (sname,rid) values('$sname',$rid)"; 7 var_dump($sql); 8 $r = $pdo->query($sql); 9 var_dump($r); 10 header("Location: index.php"); 11 } 12 else{ 13 $sql = "select rid,rnum from room"; 14 $res = $pdo->query($sql);//结果集 15 $ros = $res->fetchAll(PDO::FETCH_ASSOC); 16 require "add.html"; 17 } 18 19 ?>

1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title></title> 5 </head> 6 <body> 7 <h5>学生添加</h5> 8 <form method="post"> 9 姓名<input type="text" name="sname" ><br/> 10 宿舍 11 <select name="rid"> 12 <?php foreach ($ros as $r) {?> 13 <option value="<?php echo $r['rid']; ?>"><?php echo $r["rnum"]; ?></option> 14 <?php } ?> 15 </select> 16 <input type="submit" name=""> 17 </form> 18 </body> 19 </html>

1 <?php 2 require "../db.php"; 3 $sid = $_GET["sid"]; 4 //select * from stu where rid = 7 5 $sql = "select * from room where rid=$rid"; 6 $rstu=$pdo->query($sql); 7 $rstu->rowCount();//查询结果集的个数 8 if($rstu->rowCount()>0){ 9 echo "无法删除"; 10 } 11 else{ 12 $sql = "delete from stu where rid=$rid"; 13 $pdo->query($sql); 14 header('Location:index.php'); 15 } 16 ?>

1 <?php 2 require "../db.php"; 3 if(isset($_GET["sid"]) == false){ //(!isset($_GET["sid"])) 4 header('Location:index.php'); 5 } 6 $sid = $_GET["sid"]; 7 8 if($_POST){ 9 $sn = $_POST["sname"]; 10 $rid = $_POST["rid"]; 11 $sql = "update stu set sname='$sn',rid=$rid where stuid = $sid "; 12 $pdo->query($sql); 13 header('Location:index.php'); 14 } 15 else{ 16 17 $sql = "select * from stu where stuid=$sid"; 18 $rstu = $pdo->query($sql); 19 //var_dump($rstu); 20 //获取结果集中的一个对象 一行数据 21 $stu = $rstu->fetch(PDO::FETCH_ASSOC); 22 var_dump($stu); 23 echo "<hr>"; 24 $sql = "select rid,rnum from room"; 25 $res = $pdo->query($sql);//结果集 26 $ros = $res->fetchAll(PDO::FETCH_ASSOC); 27 var_dump($ros); 28 } 29 30 ?> 31 <form method="post"> 32 <input type="" name="sname" value="<?php echo $stu['sname'] ?>"> 33 <select name="rid"> 34 <?php foreach ($ros as $r) { ?> 35 <option 36 value="<?php echo $r['rid']; ?>" 37 <?php echo $stu['rid']==$r["rid"]?"selected":"";?> 38 > 39 <?php echo $r["rnum"]; ?> 40 </option> 41 <?php } ?> 42 </select> 43 <input type="submit" name=""> 44 </form>
新增功能:批量修改

1 <?php 2 require "../db.php"; 3 if($_POST){ 4 //选中 将要修改的 学生主键id 5 $stuids = $_POST["stuid"]; 6 //修改之后宿舍主键id 7 $rid = $_POST["rid"]; 8 var_dump($stuids); 9 foreach ($stuids as $sid) { 10 $sql = "update stu set rid = $rid where stuid=$sid"; 11 $res = $pdo->query($sql); 12 } 13 header('Location:index.php'); 14 } 15 else { 16 //宿舍查询 17 $sql = "select rid,rnum from room"; 18 $res = $pdo->query($sql);//结果集 19 $ros = $res->fetchAll(PDO::FETCH_ASSOC); 20 //var_dump($ros); 21 //学生查询 22 $sql = "select stuid,sname from stu"; 23 $res = $pdo->query($sql);//结果集 24 $rsts = $res->fetchAll(PDO::FETCH_ASSOC); 25 var_dump($rsts); 26 } 27 ?> 28 <h2>批量修改学生宿舍</h2> 29 <form method="post"> 30 <ul> 31 <?php foreach ($rsts as $s) { ?> 32 <li><input type="checkbox" name="stuid[]" value="<?php echo $s['stuid']; ?>"><?php echo $s['sname']; ?></li> 33 <?php } ?> 34 </ul> 35 <select name="rid"> 36 <?php foreach ($ros as $r) { ?> 37 <option value="<?php echo $r['rid']; ?>"> 38 <?php echo $r["rnum"]; ?> 39 </option> 40 <?php } ?> 41 42 </select> 43 <input type="submit" name=""> 44 </form> 45 <!-- 46 1 数据库 表 宿舍表 学生表 1 v N 参考 studata.sql 47 48 2 分别查询 学生表 与 宿舍表 参考 all.php 8--15行 49 2.1 sql查询语句 50 2.2 query执行 返回结果集 51 2.3 结果集转换 数组 fetchAll fetch区分 52 53 3 渲染页面 循环 宿舍 与 学生 数组 54 3.1 foreach li tr(html) all.php 22-24 55 3.2 https://www.runoob.com/try/try.php?filename=tryhtml_checkbox 复选 56 3.3 checkbox name带有[] value $_POST["stuid"] 获取数组 57 3.4 slect value 与 文本值 可以不同 58 <input type="text" name="" value="11111"> 59 3.5 详细思路 遍历参考 test.php 60 61 <select> 62 <option value="111">aaaa</option> 63 </select> 64 65 4 修改 update stu set rid = 11 where stuid = 10 66 67 68 69 -->