php 增删改查范例(1)

时间:2022-10-20 23:42:51

主页(含多条件查询):

<?php
$db = new Mysqli("localhost","root","root","db_0808");
//!empty(mysqli_connect_error())?die("连接错误"):"";
$sql = "select * from user where is_delete = 0 ";
if(!empty($_POST)){
    $name=$_POST['name'];
    $sex=$_POST['sex'];
    $birth_start=$_POST['birth_start'];
    $birth_end=$_POST['birth_end'];
    $sql.=!empty($name)?"and name like '%{$name}%'":"";
    $sql.=($sex!='')? "and sex like '{$sex}'":"";
    if(!empty($birth_start)&&!empty($birth_end)){
        $sql.="and birthday > '{$birth_start}'and birthday < '{$birth_end}'";
    }
}
//var_dump($_POST);
//var_dump($data);
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="../js/WdatePicker.js"></script>
</head>
<body>
<FORM action="index.php" method="post">
    <table>
        <tr>
            <td>姓名:<input type ="text" name="name"></td>
            <td>性别:
                <select name="sex">
                    <option value="1">男</option>
                    <option value="0">女</option>
                </select>
            </td>
            <td>
                出生年月:<input type="text" name="birth_start" class="Wdate" onClick="WdatePicker({el:this,dateFmt:'yyyy-MM-dd'})">
                ----<input type="text" name="birth_end" class="Wdate" onClick="WdatePicker({el:this,dateFmt:'yyyy-MM-dd'})">
            </td>
            <td>
                <input type="submit" value="查询">
            </td>
        </tr>
    </table>
</FORM>
<form action="batch_delete.php" method="post">
<table border="1">
    <tr>
        <td>ID</td>
        <td>姓名</td>
        <td>性别</td>
        <td>出生年月</td>
        <td>年龄</td>
        <td>操作</td>
    </tr>
    <?php
        $result = $db->query($sql);
        while ($data = $result->fetch_row()){
            //var_dump($data);
            if($data[2] == 1){
                $data[2] = "男";
            }else if($data[2]== 0 ) {
                $data[2] = "女";
            }else{
                $data[2] = "保密";
            }
            echo "<tr>
                       <td>{$data[0]}</td>
                       <td>{$data[1]}<input type='checkbox' name='ids[]' value='{$data[0]}'></td>
                       <td>{$data[2]}</td>
                       <td>{$data[3]}</td>
                       <td>{$data[4]}</td>
                       <td><a href='delete.php?id={$data[0]}'>删除</a>&nbsp;&nbsp;<a href='edit.php?id={$data[0]}'>编辑</a></td>
                   </tr>";
        }
    ?>
</table>
    <input type="submit" value="批量删除">
</form>
<a href="add.php">新增用户</a>
</body>
</html>