• php实现的mysql的简单查询删除


    环境:php5.6+mysql+Apache

    后端代码:

    <?php 
        // header('Content-Type:text/json;charset=utf-8');
        $con=mysqli_connect("数据库地址:端口","用户名","密码","数据库名"); 
        if (!$con) 
        { 
            die("连接错误: " . mysqli_connect_error()); 
        }else{
            echo "成功";
        }
    
    $action = $_GET['action'];
    
    switch ($action) {
        case 'del_row':
            del_row();//删除
            break;
    
        case 'edit_row':
            edit_row();//查询
            break;
    }
    
    function del_row(){
        global $con;
        $dataid = $_POST['dataid'];
        $sql = "delete from `order` where `id`='".$dataid."'";
        if (mysqli_query($con,$sql)) {
            echo 'ok';
        }else{
            echo '删除失败';
        }
    }
    
    function edit_row(){
        global $con;
        $sql = "SELECT id, name, mobile FROM `user_address`";//sql查询语句
        $result = $con->query($sql);//获得查询结果
         
        if ($result->num_rows > 0) {
            // 输出每行数据
            while($row = $result->fetch_assoc()) {
                $results[] = $row;
            }
        } else {
            echo "0 results";
        }
        echo json_encode($results);
    }
    ?>

    前端代码

    $.ajax({
            type: "GET",
            url: "./data.php?action=edit_row",
            data: '',
            dataType: "json",
            success: function(data){
                var len = data.length,
                    str = '', item;
                if(len>0) {
                    for (var i = 0; i < len; i++) {
                        item = data[i];
                        str += '<tr>' +
                            '<td>' + item.id + '</td>' +
                            '<td>' + item.name + '</td>' +
                            '<td>' + item.mobile + '</td>' +
                            '<td>' + item.city + '</td>' +
                            '<td>' + item.country + '</td>' +
                            '<td>' + item.detail + '</td>' +
                            '<td>' + '<span class="order-btn">'+'删除'+'</span>' + '</td>' +
                            '</tr>';
                    }
                    $('#order-table1').append(str);
                }
            }
        });
  • 相关阅读:
    linux命令学习
    linux sar命令详解
    消息中间件设计
    google三驾马车
    Apache ZooKeeper 服务启动源码解释
    ubuntu16 ccls neovim coc.nvim ccls langserver安装
    ubuntu 字体安装 —— 以nerd font为例
    neovim
    vim youcompleteme conda 虚拟环境
    sublime 插件管理
  • 原文地址:https://www.cnblogs.com/lqzweb/p/7101743.html
Copyright © 2020-2023  润新知