首页 > 代码库 > php实现的mysql的简单查询删除

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);
            }
        }
    });

 

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