修改
1、首先,建立一个修改页面(edit.php),加入表单(也可与增加页面的表单同用)
<?php $id = $_GET['id']; $db = new Mysqli("localhost","root","root","student"); $sql = "select * from student.student WHERE Sno = {$id}"; $result = $db->query($sql); $date = $result->fetch_assoc(); //var_dump($date); ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form method="post" action="editpost.php"> <input type="hidden" name="Sno" placeholder="id" value="<?php echo $date['Sno']?>"> 姓名:<input type="text" name="Sname" placeholder="姓名" value="<?php echo $date['Sname']?>" > 性别: <input type="radio" name="Ssex" value="1" <?php if ($date['Ssex'] == 1){echo 'checked';}?> id="man"><label for="man">男</label> <input type="radio" name="Ssex" value="0" <?php if ($date['Ssex'] == 0){echo 'checked';}?> id="woman"><label for="woman">女</label> 出生年月: <input type="text" name="Sbirthday" placeholder="出生年月" value="<?php echo $date['Sname']?>"> 班级: <input type="text" name="Class" placeholder="班级" value="<?php echo $date['Class']?>"> <input type="submit" value="确认修改"> </form> </body> </html>
2、建立一个之前写到的需要作出修改动作的页面(editpost.php)
<?php $id = $_POST['Sno']; $name = $_POST['Sname']; $sex = $_POST['Ssex']; $birthday = $_POST['Sbirthday']; $class = $_POST['Class']; $db = new Mysqli("localhost","root","root","student"); $sql = "update student set Sname='{$name}',Ssex='{$sex}',Sbirthday='{$birthday}',Class='{$class}' WHERE Sno='{$id}'"; if ($db->query($sql)){ header("location:crud.php"); }
之后就可以进行测试了
测试一下:
1、在首页点击修改(选择修改ID:111行)
2、可以成功提取 id 及 信息
3、对内容进行修改
4、点击确认看一下
5、成功^-^