2016/3/26 连接数据库 网页中数据的增删改 add delete update addchuli updateChuLi test8 DBDA

主页面 test8.php

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Document</title>
 6 </head>
 7 <body>
 8 <table width=100% border="1" cellpadding="0" cellspacing="0">
 9     <tr>
10         <td>代号</td>
11         <td>姓名</td>
12         <td>性别</td>
13         <td>民族</td>
14         <td>生日</td>
15         <td>操作</td>
16     </tr>
17     <?php 
18     //造连接对象
19     $db=new mysqli("localhost","root","123","test2");
20     //判断连接是否成功
21     !mysqli_connect_error()or die("连接失败!");
22     //写sql语句
23     $sql="select * from Info";
24     //执行sql语句
25     $result=$db->query($sql);
26     //处理查询的结果
27     $attr=$result->fetch_all();
28     for ($i=0; $i <count($attr) ; $i++) { 
29             echo "<tr>";
30             for ($j=0; $j <count($attr[$i]);$j++)
31              { 
32                 echo "<td>{$attr[$i][$j]}</td>";
33              }
34     //GET提交方式手写出来拼接出来
35     //'delete.php?code={$attr[$i][0]}'
36             echo "<td><a href='delete.php?code={$attr[$i][0]}'>删除</a><a href='update.php?code={$attr[$i][0]}'>修改</a></td>";
37             echo "</tr>";
38         }
39 
40 
41      ?>
42 </table>
43 <br/>
44 <a href="Add.php"><input type="button" value="添加数据"/></a>
45 </body>
46 </html>

数据库封装成类 页面  DBDA.php

 1 <?php 
 2 $code=$_POST["code"];
 3 $name=$_POST["name"];
 4 $sex=$_POST["sex"];
 5 $nation=$_POST["nation"];
 6 $birthday=$_POST["birthday"];
 7 
 8 //造连接对象
 9 $db=new mysqli("localhost","root","123","test2");
10 //判断是否出错
11 !mysqli_connect_error() or die("连接失败!");
12 //写sql语句
13 $sql="update info set name='$name',sex= $sex,nation='$nation',birthday='$birthday'where code='$code'";//sex= $sex 返回布尔型值 不能加引  否则无法把女改男
14 //执行语句
15 $result=$db->query($sql);
16 if ($result) {
17     header("location:test8.php");
18 }
19 else
20 {
21     echo"修改失败!";
22 }
23  ?>}
View Code

Add.php

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Document</title>
 6 </head>
 7 <body>
 8 <form action="Addchuli.php" method="post">
 9 <div>
10     代号:
11     <input type="text" name="code"/>
12     <!-- 提交必有name -->
13 </div>
14 <div>
15     姓名:
16     <input type="text" name="name"/>
17 </div>
18 <div>
19     性别:
20     男<input type="radio" value="true" name="sex" checked="checked" />&nbsp;
21     女<input type="radio" value="false"name="sex"/>
22 </div>
23 <div>
24     民族:
25     <select name="nation" >
26         <?php
27             //造连接对象
28         $db=new mysqli("localhost","root","123","test2");
29         //判断连接是否成功
30         !mysqli_connect_error()or die("连接失败!");
31         //写sql语句
32         $sql="select * from Nation";
33         //出现提示fetch_row/all错误 找sql语句可能表错符号空格多少的问题
34         //执行sql语句
35         $result=$db->query($sql);
36         //处理查询的结果
37         $attr=$result->fetch_all();
38         for ($i=0; $i <count($attr) ; $i++) { 
39             
40                 echo "<option value='{$attr[$i][0]}'>{$attr[$i][1]}</option>";
41         }
42         ?>
43     </select>
44 </div>
45 <div>
46     生日:
47     <input type="text" name="birthday"/>
48 </div>
49 <div>
50     <input type="submit" value="确定"/>
51     <a href="test8.php">返回主页</a>
52 </div>
53 </form>
54 </body>
55 </html>
View Code

Addchuli.php

 1 <?php
 2 $code=$_POST["code"];
 3 $name=$_POST["name"];
 4 $sex=$_POST["sex"];
 5 $nation=$_POST["nation"];
 6 $birthday=$_POST["birthday"];
 7 
 8 //造连接对象
 9 $db=new mysqli("localhost","root","123","test2");
10 //判断是否出错
11 !mysqli_connect_error() or die("连接失败!");
12 //写sql语句
13 $sql="insert into Info values('$code','$name',$sex,'$nation','$birthday')";
14 //执行语句
15 $result=$db->query($sql);
16 if ($result) {
17     header("location:Add.php");
18 }
19 else{
20     echo"执行失败!";
21 }
22 
23 ?>
View Code

Update.php

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Document</title>
 6 </head>
 7 <body>
 8 <?php 
 9         $code=$_GET["code"];
10         
11         $db=new mysqli("localhost","root","123","test2");
12         //判断连接是否成功
13         !mysqli_connect_error()or die("连接失败!");
14         //写sql语句
15         $sqlx="select * from Info where Code='$code'";
16         //执行sql语句
17         $result=$db->query($sqlx);
18         $attx=$result->fetch_row();
19  ?>
20 <form action="UpdateChuLi.php" method="post">
21 <div>
22     代号:
23     <input readonly="readonly" type="text" name="code" value="<?php echo $attx[0]; ?>"/>
24 </div>
25 <div>
26     姓名:
27     <input type="text" name="name" value="<?php echo $attx[1]; ?>";/>
28 </div>
29 <div>
30     性别:
31     男<input type="radio" value="true" name="sex" <?php echo $attx[2]?"checked='checked'":""; ?> />&nbsp;
32     女<input type="radio" value="false" name="sex" <?php echo $attx[2]?"":"checked='checked'"; ?>/>
33 </div>
34 <div>
35     民族:
36     <select name="nation" >
37         <?php
38         // 上面有了连接对象,下面这个可以删掉前两句
39         //造连接对象
40         //$db=new mysqli("localhost","root","123","test2");
41         //判断连接是否成功
42         //!mysqli_connect_error()or die("连接失败!");
43         //写sql语句
44         $sql="select * from nation";
45         //执行sql语句
46         $result=$db->query($sql);
47         //处理查询的结果
48         $attr=$result->fetch_all();
49         for ($i=0; $i <count($attr) ; $i++) { 
50                 if ($attx[3]==$attr[$i][0]) 
51                 {
52                     echo"<option selected='selected' value='{$attr[$i][0]}'>{$attr[$i][1]}</option>";
53                 }
54                 else
55                 {
56                 echo "<option value='{$attr[$i][0]}'>{$attr[$i][1]}</option>";
57                 }
58         }
59         ?>
60     </select>
61 </div>
62 <div>
63     生日:
64     <input type="text" name="birthday" value="<?php echo $attx[4]; ?>" />
65 </div>
66 <div>
67     <input type="submit" value="确定"/>
68     <a href="test8.php">返回主页</a>
69 </div>
70 </form>
71 </body>
72 </html>
View Code

UpadateChuLi.php

 1 <?php 
 2 $code=$_POST["code"];
 3 $name=$_POST["name"];
 4 $sex=$_POST["sex"];
 5 $nation=$_POST["nation"];
 6 $birthday=$_POST["birthday"];
 7 
 8 //造连接对象
 9 $db=new mysqli("localhost","root","123","test2");
10 //判断是否出错
11 !mysqli_connect_error() or die("连接失败!");
12 //写sql语句
13 $sql="update info set name='$name',sex= $sex,nation='$nation',birthday='$birthday'where code='$code'";//sex= $sex 返回布尔型值 不能加引  否则无法把女改男
14 //执行语句
15 $result=$db->query($sql);
16 if ($result) {
17     header("location:test8.php");
18 }
19 else
20 {
21     echo"修改失败!";
22 }
23  ?>}
View Code

delete.php

 1 <?php 
 2 $code=$_GET["code"];
 3 $db=new mysqli("localhost","root","123","test2");
 4         //判断连接是否成功
 5         !mysqli_connect_error()or die("连接失败!");
 6         //写sql语句
 7         $sql="delete from Info where code='$code'";
 8         //执行sql语句
 9         $result=$db->query($sql);
10         if ($result) {
11             header("location:test8.php");
12         }
13         else
14         {
15             echo "删除失败!";
16         }
17  ?>
View Code

显示效果:

主页表:

增加:

删除:

 

修改:

posted on 2016-03-28 00:16  一棵树2016  阅读(226)  评论(0编辑  收藏  举报