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

main页面(主页面)

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($attras$v)
{
    $sex = $v[2]? '男':'女';
    $sqln = "select Name from Nation where Code ='$v[3]'";
    $rnation = $db ->query($sqln);
    $attrn = $rnation->fetch_assoc();
    echo "";
}
?>

代号 姓名 性别 民族 生日 操作
{$v[0]} {$v[1]} {$sex} {$attrn['Name']} {$v[4]} 删除 修改

Add(添加数据页面)

 1 

添加数据

2
3
代号:
4
姓名:
5
性别: 6 7 8
9
民族: 10 23
24
生日:
25
26

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 

修改数据

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
12
代号:$att1[0] ?>" />
13
姓名:$att1[1] ?>" />
14
性别: 15 echo $att1[2] ? "checked='checked'" : ""; ?> />16 echo $att1[2] ? "" : "checked='checked'"; ?> />17
18
民族: 19 40
41
生日:$att1[4] ?>"/>
42
43

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 }

 

转载于:https://www.cnblogs.com/zk0533/p/5459923.html

你可能感兴趣的:(PHP 页面中实现数据的增删改查)