drlma06060 2017-10-03 11:27
浏览 28
已采纳

MySQL使用Ajax返回表名而不是值

I'm trying to make an AJAX request to fetch some data from the database but the only thing returned is the name of the column. Can somebody explain why? Here is my code:

=> Table Name :- tblstudents

id = 0
fname = john
lname = doe
tel = 555-564-1585

id = 1 
fname = paul
lname = smith
tel = 555-134-5644

id = 2
fname = laura
lname = mcdo
tel = 555-465-7512

=> AJAX method:

function fetchFromDBPHP(column, fname, id, tel) {
    $.ajax({
        type: "post",
        url: "./php/fetchFromDB.php",
        dataType: 'json',
        data: { column: column, fname: fname, id: id },
        success: function(data) {
            localStorage.setItem(tel, data);
        },
        error:function(request, status, error) {
            console.log("** Error from fetchFromDBPHP **");
            console.log("Error: " + error + "
Message: " + request.responseText);
        }
    });
}

=> Javascript :

fetchFromDBPHP(column, fname, id, "one");
var result = localStorage.getItem("one");
console.log("Result: " + result);

=> PHP :

<?php
    $column = $_POST['column'];
    $fname = $_POST['fname'];
    $id = $_POST['id'];

    if (isset($column)) {
        $sql = "SELECT '$column' FROM tblstudents WHERE fname = '$fname' AND id = '" . intval($id) . "'";
        $con = mysqli_connect("localhost", "root", "", "test");
        if (!$con) {
            die("Connection failed: " . mysqli_error($con));
        }
        $result = mysqli_query($con, $sql);
        $to_encode = array();
        while($row = mysqli_fetch_array($result, MYSQLI_NUM)) {
            $to_encode[] = $row;
        }
        echo json_encode($to_encode);
        mysqli_close($con);
    }
?>

As you might know, column, fname and id have values in the Javascript code. As my database is way longer, I tried to be as much close as possible to my real code. The only thing is, the result of the AJAX request gives me a JSON object result containing the name of the column, and not its content. Anybody can help? Thanks in advance :)

  • 写回答

1条回答 默认 最新

  • douxie1692 2017-10-03 12:09
    关注

    You're using the wrong quotes in your query.

    "SELECT '$column' FROM ..."
    

    ...will simply return the value of the variable $column instead of the value of the actual database column.

    Changing it to (back ticks):

    "SELECT `$column` FROM ..."
    

    will work.

    An important note...

    ...the posted code is wide open to SQL Injections and should use parameterized Prepared Statements instead of concatenation of the variables in the query. Specially since the user inputs aren't escaped at all.

    Rule of thumb, never ever trust user inputs.

    Regarding the column name, which you can't parameterize, you should create a white list with allowed column names.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)