php 增删改查范例(3)

时间:2022-10-21 00:24:39

编辑页面edit.php:

<?php
$id=$_GET['id'];
$db= new mysqli('localhost','root','root','db_0808');
$sql="select * from `user` where id={$id} ";
$result=$db->query($sql);
$data=$result->fetch_assoc();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="editpost.pop" method="get">
    <input type="hidden" name="id" value="<?php echo $data['id']?>">
    <input type="text" name="name" placeholder="姓名" value="<?php echo $data['name']?>">
    <input type="radio" name="sex" value="1" <?php if ($data['sex'==1]){echo 'checked';} ?> id="man"><label for="man">男</label>
    <input type="radio" name="sex" value="0" <?php if ($data['sex'==0]){echo 'checked';} ?> id="woman"><label for="woman">女</label>
    <input type="text" name="age" value="<?php echo $data['age']?>" placeholder="年龄">
    <input type="text" name="birthday"  value="<?php echo $data['birthday']?>" placeholder="出生年月">
    <input type="submit" value="提交">
</form>
</body>
</html>
编辑处理页面editpost.pop:

<?php
$id=$_POST['id'];
$name = $_POST['name'];
$sex = $_POST['sex'];
$age = $_POST['age'];
$birthday = $_POST['birthday'];
$db = new Mysqli("localhost","root","root","db_0808");
$sql = "update `user` set name='{$name}',sex='{$age}',age='{$age}',birthday='{$birthday}'";
if($db->query($sql)){
    echo "执行成功";
}
?>