php数据访问(三)修改数据

info表

nation表

四.修改数据

 

 

 

1.显示info表信息页面 main.php

 1 <body>
 2 <h1>显示info信息</h1>
 3 <table width="100%" border="1" cellpadding="0" cellspacing="0">
 4     <tr>
 5         <td>代号</td>
 6         <td>姓名</td>
 7         <td>性别</td>
 8         <td>生日</td>
 9         <td>民族</td>
10         <td>操作</td>
11     </tr>
12     
13     <?php
14     $db = new MySQLi("localhost","root","123","test_123");
15     $sql = "select * from info";
16     $result = $db->query($sql);
17     $arr = $result->fetch_all();
18     foreach($arr as $v)
19     {
20         //修改性别
21         $sex = $v[4]?"男":"女";
22         
23         //修改民族
24         $sql1 = "select nno from nation where code='{$v[6]}'";
25         $r1 = $db->query($sql1);
26         $a1 = $r1->fetch_row();
27         
28         echo "<tr>
29                  <td>{$v[2]}</td>
30                 <td>{$v[3]}</td>
31                 <td>{$sex}</td>
32                 <td>{$v[5]}</td>
33                 <td>{$a1[0]}</td>
34                 <td><a href='del.php?code={$v[2]}' onclick=\"return confirm('确认删除么?')\">删除</a>
35                     <a href='update.php?code={$v[2]}'>修改</a>                
36                 </td>
37               </tr>";
38     }
39     ?>
40     
41 </table>
42 </body>
43 <script type="text/javascript">
44 //confirm("确定删除么");
45 </script>

2.点击进入修改页面 update.php

 1 <body>
 2 <?php
 3 //修改数值是不允许修改主键值的
 4 $code = $_GET["code"];
 5 $db = new MySQLi("localhost","root","123","test_123");
 6 $sql = "select * from info where code='{$code}'";
 7 $result = $db->query($sql);
 8 $arr = $result->fetch_row();
 9 
10 ?>
11 
12 <h1>修改数据</h1>
13 <form action="xiugai.php" method="post">
14 <div><input type="hidden" name="code" value="<?php echo $arr[2];?>"/></div> <!--把代号隐藏,用户不能修改主键但依然可以提交-->
15 
16 <div>姓名:<input type="text" name="name" value="<?php echo $arr[3];?>"/></div>
17 
18 <div>
19 性别:
20<input type="radio" name="sex" value="1" <?php echo $arr[4]?"checked='checked'":""; ?> />
21<input type="radio" name="sex" value="0" <?php echo $arr[4]?"":"checked='checked'"; ?> />
22 </div>
23 
24 <div>
25 民族:
26 <select name="nation">
27 <?php
28 $sqln = "select * from nation";
29 $resultn = $db->query($sqln);
30 $arrn = $resultn->fetch_all();
31 foreach($arrn as $v)
32 {
33     $arr[6];//该人员的民族代号,看的是info表
34     $v[0];//即将造的option的民族代号,看的是nation表
35     if($arr[6]==$v[0])//判断点击到修改页面后还是默认民族值
36     {
37         echo "<option selected='selected' value='{$v[0]}'>{$v[1]}</option>";
38     }
39     else
40     {
41         echo "<option value='{$v[0]}'>{$v[1]}</option>";
42     }
43 }
44 ?>
45 </select>
46 </div>
47 <div>生日:<input type="text" name="birthday" value="<?php echo $arr[5];?>"/></div> 
48 <input type="submit" value="修改" />
49 </form>
50 </body>

3.修改处理页面 xiugai.php 修改后直接返回main.php页面

 1 <?php
 2 header("Content-type:text/html;charset=utf-8");
 3 $code = $_POST["code"];
 4 $name = $_POST["name"];
 5 $sex = $_POST["sex"];
 6 $nation = $_POST["nation"];
 7 $birthday = $_POST["birthday"];
 8 
 9 $db = new MySQLi("localhost","root","123","test_123");
10 $sql = "update info set name='{$name}',sex='{$sex}',nation='{$nation}',birthday='{$birthday}' where code='{$code}'";//主键值不修改作为条件放在后面
11 
12 if($db->query($sql))
13 {
14     header("location:main.php");
15 }
16 else
17 {
18     echo "修改失败";
19 }

 

posted @ 2017-04-27 16:27  ChrissZhao  阅读(252)  评论(0编辑  收藏  举报