1. 程式人生 > 資料庫 >資料庫:宿舍表和學生表——主鍵與外來鍵聯絡

資料庫:宿舍表和學生表——主鍵與外來鍵聯絡

第一步:建一個資料夾——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
studata.sql

 

 

 

第三步:建一個頁面——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 }
db.php

 

第四步:在資料夾1105中新建兩個資料夾——room、stu。

第五步:分別在資料夾room、stu中編寫實現增、刪、查、改功能的程式碼。具體如下:

    room中的增、刪、查、改:

index.php

 

index.html

 

add.php

 

add.html

 

del.php

 

           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>
index.php

 

 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  ?>
add.php
 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>
add.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  ?>
del.php
 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>
up.php

新增功能:批量修改

 1 <?php 
 2     require "../db.php";
 3     if($_POST){
 4         var_dump($_POST["stuid"]);
 5     }
 6     else {
 7         //宿舍查詢
 8         $sql = "select rid,rnum from room";
 9         $res = $pdo->query($sql);//結果集
10         $ros = $res->fetchAll(PDO::FETCH_ASSOC);
11         //var_dump($ros);
12         //學生查詢
13         $sql = "select stuid,sname from stu";
14         $res = $pdo->query($sql);//結果集
15         $rsts = $res->fetchAll(PDO::FETCH_ASSOC);
16         //var_dump($rsts);
17     }
18  ?>
19  <h2>批量修改學生宿舍</h2>
20  <form method="post">
21      <ul>
22      <?php foreach ($rsts as $s) { ?>
23          <li><input type="checkbox" name="stuid[]" value="<?php echo $s['stuid']; ?>"><?php echo $s['sname']; ?></li>
24      <?php } ?>
25   </ul>
26      <select name="rid">
27     <?php foreach ($ros as $r) { ?>
28         <option value="<?php echo $r['rid']; ?>">
29             <?php echo $r["rnum"]; ?>
30         </option>
31     <?php } ?>
32 
33 </select>
34 <input type="submit" name="">
35 </form>
36 <!-- 
37 1 資料庫 表 宿舍表  學生表 1 v N  參考 studata.sql
38 2 分別查詢 學生表 與 宿舍表 參考 all.php 8--15行 
39   2.1 sql查詢語句 
40   2.2 query執行 返回結果集
41   2.3 結果集轉換 陣列 fetchAll fetch區分
42 
43 3 渲染頁面 迴圈 宿舍 與 學生  陣列
44   3.1 foreach li tr(html) all.php 22-24
45   3.2 https://www.runoob.com/try/try.php?filename=tryhtml_checkbox 複選
46   3.3 name帶有[]  value
47   3.4 slect value 與 文字值 可以不同
48  -->
all.php