php增删改查

Posted 静静地开始

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php增删改查相关的知识,希望对你有一定的参考价值。

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    .adc {
        color: red;
    }
</style>

<body>
    <?php
    // 分页
    if (isset($_GET[‘page‘])) {
        $p = $_GET[‘page‘];
    } else {
        $p = 1;
    };
    $pagenum = 2;
    $pages = ($p - 1) * $pagenum;
    $page = $pages . ‘,‘ . $pagenum;
    // p标签查询条件
    $where = "";
    $tit ="";
    if(isset($_GET[‘tit‘])){
        $tit = $_GET[‘tit‘];
        $where = "where title like ‘%$tit%‘";
    }
    // 
    $conn = new mysqli(‘localhost‘, ‘root‘, ‘root‘, ‘aaa‘);
    $sql = "select * from chanpinbiao $where $where limit $page";
    $res = $conn->query($sql);
    $arr = $res->fetch_all(MYSQLI_ASSOC);
    // 查询所有数据
    $sql = "select * from chanpinbiao $where";
    $ree = $conn->query($sql);
    $zu = $ree->num_rows;
    // 分多少页
    $zui = ceil($zu / $pagenum);
    $conn->close();
    ?>
    <p>
        <form action="" method="get">
            请输入内容:<input type="text" name="tit" value="<?php echo $tit ?>">
            <input type="submit" value="查询">
        </form>
    </p>
    <table border="1">
        <tr>
            <td>id</td>
            <td>标题</td>
            <td>图片</td>
            <td>内容</td>
            <td>操作</td>
        </tr>
        <?php
        foreach ($arr as $var) {
        ?>
            <tr>
                <td><?php echo $var[‘id‘] ?></td>
                <td><?php echo $var[‘title‘] ?></td>
                <td>
                    <?php
                    if ($var[‘img‘]) {
                    ?>
                        <img src="<?php echo $var[‘img‘] ?>">
                    <?php
                    } else {
                        echo ‘暂无图片‘;
                    };
                    ?>
                </td>
                <td><?php echo $var[‘cont‘] ?></td>
                <td>
                    <a href="add.php">添加</a>
                    <a href="javasript:;" class="del" tt="<?php echo $var[‘id‘] ?>">删除</a>
                </td>
            </tr>
        <?php
        }
        ?>


    </table>
    <p>
        <a href="tupian.php?page=1&tit=<?php echo $tit ?>">首页</a>
        <?php if ($p > 1) { ?>
            <a href="tupian.php?page=<?php echo ($p - 1); ?>&tit=<?php echo $tit ?>">上一页</a>
        <?php
        }
        for ($i = 1; $i <= $zui; $i++) {
        ?>
            <a href="tupian.php?page=<?php echo $i; ?>&tit=<?php echo $tit ?>" <?php if ($i == $p) { ?>class="abc" <?php } ?>><?php echo $i; ?></a>
        <?php
        };
        if ($zui > $p) {
        ?>
            <a href="tupian.php?page=<?php echo ($p + 1); ?>&tit=<?php echo $tit ?>">下一页</a>
        <?php
        }
        ?>
        <a href="tupian.php?page=<?php echo $zui; ?>&tit=<?php echo $tit ?>">尾页</a>
    </p>
</body>
<script src="js/jquery-3.4.1.js"></script>
<script>
    $(‘.del‘).click(function() {
        var i = confirm(‘确定删除?‘);
        var ids = $(this).attr(‘tt‘);
        var thid = $(this);
        if (i == true) {
            $.ajax({
                url: der.php,
                date: {
                    ‘id‘: ids
                },
                dataType: ‘json‘,
                type: ‘post‘,
                success: function(res) {
                    if (res.code == 1) {
                        this.parents(‘tr‘).remove();
                    } else {
                        alert(‘服务器异常,稍后再试‘);
                    }
                }
            })
        };
    });
</script>

</html>

以上是关于php增删改查的主要内容,如果未能解决你的问题,请参考以下文章

Laravel框架——增删改查

库表的增删改查

mysql数据库增删改查

mysql增删改查

django配置模型增删改查

MySQL基本操作--库表增删改查