main頁面(主頁面)
<table width="100%" border="1" cellpadding="0" cellspacing="0"> <tr> <td>代號</td> <td>姓名</td> <td>性別</td> <td>民族</td> <td>生日</td> <td>操作</td> </tr> <?php $db = new MySQLi("localhost","root","","mydb"); !mysqli_connect_error() or die("連接失敗"); $sql = "select * from Info"; $result = $db->query($sql); $attr = $result->fetch_all(); foreach($attr as $v) { $sex = $v[2]? '男':'女'; $sqln = "select Name from Nation where Code ='$v[3]'"; $rnation = $db ->query($sqln); $attrn = $rnation->fetch_assoc(); echo "<tr> <td>{$v[0]}</td> <td>{$v[1]}</td> <td>{$sex}</td> <td>{$attrn['Name']}</td> <td>{$v[4]}</td> <td> <a href='Delete.php?code={$v[0]}'>刪除</a> <a href='Update.php?code={$v[0]}'>修改</a> </td> </tr>"; } ?> </table> <div><a href="Add.php">添加數據</a></div>
Add(添加數據頁面)
1 <h1>添加數據</h1> 2 <form action="AddChuLi.php" method="post"> 3 <div>代號:<input type="text" name="code" /></div> 4 <div>姓名:<input type="text" name="name" /></div> 5 <div>性別: 6 <input type="radio" value="男" name="sex" />男 7 <input type="radio" value="女" name="sex" />女 8 </div> 9 <div>民族: 10 <select name="nation"> 11 <?php 12 $db = new MySQLi("localhost","root","","mydb"); 13 !mysqli_connect_error() or die("連接失敗!"); 14 $sql = "select * from Nation"; 15 $r = $db->query($sql); 16 $att = $r->fetch_all(); 17 foreach($att as $v) 18 { 19 echo "<option value='{$v[0]}'>{$v[1]}</option>"; 20 } 21 ?> 22 </select> 23 </div> 24 <div>生日:<input type="text" name="birthday" /></div> 25 <div><input type="submit" value="添加數據" /></div> 26 </form>
AddChuLi頁面(添加數據處理頁面)
1 <?php 2 $code = $_POST["code"]; 3 $name = $_POST["name"]; 4 $sex = $_POST["sex"]; 5 $s = 1; 6 if($sex =="女") 7 { 8 $s=0; 9 } 10 $nation = $_POST["nation"]; 11 $birthday = $_POST["birthday"]; 12 $db = new MySQLi("localhost","root","","mydb"); 13 !mysqli_connect_error() or die("連接失敗!"); 14 $sql = "insert into Info values('{$code}','{$name}','{$s}','{$nation}','{$birthday}')"; 15 $result = $db->query($sql); 16 if($result) 17 { 18 header("location:main.php"); 19 } 20 else 21 { 22 echo "添加失敗!"; 23 }
Delete頁面(刪除數據頁面)
1 <?php 2 $code = $_GET["code"]; 3 $db = new MySQLi("localhost","root","","mydb"); 4 !mysqli_connect_error() or die("連接失敗!"); 5 $sql = "delete from Info where Code ='{$code}'"; 6 $r = $db->query($sql); 7 if($r) 8 { 9 header("location:main.php"); 10 } 11 else 12 { 13 echo "刪除失敗!"; 14 }
Update頁面(修改數據頁面)
1 <h1>修改數據</h1> 2 <?php 3 $code = $_GET["code"]; 4 $db = new MySQLi("localhost","root","","mydb"); 5 !mysqli_connect_error() or die("連接失敗!"); 6 $sql1 = "select * from Info where Code='{$code}'"; 7 $r1 = $db->query($sql1); 8 $att1 = $r1->fetch_row(); 9 10 ?> 11 <form action="UpdateChuLi.php" method="post"> 12 <div>代號:<input type="hidden" name="code" value="<?php echo $att1[0] ?>" /></div> 13 <div>姓名:<input type="text" name="name" value="<?php echo $att1[1] ?>" /></div> 14 <div>性別: 15 <input type="radio" value="男" name="sex" <?php echo $att1[2] ? "checked='checked'" : ""; ?> />男 16 <input type="radio" value="女" name="sex" <?php echo $att1[2] ? "" : "checked='checked'"; ?> />女 17 </div> 18 <div>民族: 19 <select name="nation"> 20 <?php 21 $db = new MySQLi("localhost","root","","mydb"); 22 !mysqli_connect_error() or die("連接失敗!"); 23 $sql = "select * from Nation"; 24 $r = $db->query($sql); 25 $att = $r->fetch_all(); 26 foreach($att as $v) 27 { 28 if($att1[3]==$v[0]) 29 { 30 echo "<option value='{$v[0]}' selected='selectec' >{$v[1]}</option>"; 31 } 32 else 33 { 34 echo "<option value='{$v[0]}'>{$v[1]}</option>"; 35 } 36 37 } 38 ?> 39 </select> 40 </div> 41 <div>生日:<input type="text" name="birthday" value="<?php echo $att1[4] ?>"/></div> 42 <div><input type="submit" value="修改數據" /></div> 43 </form>
UpdateChuLi頁面(修改數據處理頁面)
1 <?php 2 $code = $_POST["code"]; 3 $name = $_POST["name"]; 4 $sex = $_POST["sex"]; 5 $s=1; 6 if($sex=="女") 7 { 8 $s=0; 9 } 10 $nation = $_POST["nation"]; 11 $birthday = $_POST["birthday"]; 12 $db = new MySQLi("localhost","root","","mydb"); 13 !mysqli_connect_error() or die("連接失敗!"); 14 $sql = "update Info set Name='{$name}',Sex={$s},Nation='{$nation}',Birthday='{$birthday}' where Code='{$code}'"; 15 $r = $db->query($sql); 16 if($r) 17 { 18 header("location:main.php"); 19 } 20 else 21 { 22 echo "修改失敗!"; 23 }
