PHP 页面中实现数据的增删改查

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 }

 

posted @ 2016-05-04 21:49  冷风~云  阅读(29294)  评论(0编辑  收藏  举报