php 增删改查范例(2)

增加页面add.php:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <form method="post" action="addpost.php">
        <input type="text" name="name" placeholder="姓名">
        <input type="radio" name="sex" value="1" id="man"><label for="man">男</label>
        <input type="radio" name="sex" value="0" id="woman"><label for="woman">女</label>
        <input type="text" name="age" placeholder="年龄">
        <input type="text" name="birthday" placeholder="出生年月">
        <input type="submit" value="提交">
    </form>
</body>
</html>

增加处理页面addpost.php:

<?php
$name = $_POST['name'];
$sex = $_POST['sex'];
$age = $_POST['age'];
$birthday = $_POST['birthday'];

$db = new Mysqli("localhost","root","root","db_0808");
$sql = "INSERT INTO `user` VALUES (null,'{$name}',{$sex},'{$birthday}',{$age},DEFAULT,0)";
//var_dump($db->query($sql));
if($db->query($sql)){
    header("location:index.php");
}else{
    header("location:add.php");
}
删除页面delete.php:

<?php
$id=$_GET['id'];
$db= new mysqli('localhost','root','root','db_0808');
mysqli_connect_error()?die("链接出错"):"";
//$sql="delect from user where id={$id}";
$sql2="update user set is_delete = '1' where id={$id} ";
//var_dump ($db->query($sql2));
if($db->quary(sql2)){
    header("location:index.php");
}
批量删除页面batch_delete.php:

<?php
var_dump  ($_POST['ids']);

$db = new Mysqli('localhost','root','root','db_0808');
mysqli_connect_error()?die("链接出错"):"";
//$sql =  "delete from USER WHERE id = {$id}";
//方法1
foreach($ids as $i){
$sql2 = "update user set is_delete = '1' WHERE  id = {$i}";
$result=$db->query($sql2);
}
//方法2
//$sql_str=implode(",",$ids);
//$sql2 = "update user set is_delete = '1' WHERE  id in ({$sql_str})";
//$result=$db->query($sql2);
echo "success!";
?>

原文地址:https://www.cnblogs.com/cmzhphp2017/p/7838451.html