AJAX数组与MySQL表交互的麻烦

时间:2013-11-22 15:59:02

标签: php jquery mysql ajax json

在我的网页上,我有许多与MySQL数据库中的表列相关的按钮(按钮的id和表列名完全相同)。

我想要做的是捕获数组中的ID并通过AJAX发送回与这些表列相关联的值数组,并使用相同的名称。 AJAX调用没有问题,但是我很难理解如何在我的服务器代码中转换数据以用于PHP。

我的代码如下:

jQuery的:

var username = USERNAME;
var array = [1, 2, 3, 4, 5];

$.ajax ({
    cache: false,
    url: PHP_FILE,
    dataType: "json",
    type: "post",
    data: {username: username, array: array},
    success: function(data) {
        console.log(data);
    }
});

PHP:

<?php
    $dbc = DATABASE CONNECTION;

    $username = mysqli_real_escape_string($dbc, trim($_POST['username']));
    $auth = mysqli_real_escape_string($dbc, trim($_POST['array']));

    $array = json_decode($auth, true);

    $query = "SELECT auths.".$array." FROM details INNER JOIN auths USING (code) WHERE un = '".$username."'";
    $data = mysqli_query($dbc, $query);
    $row = mysqli_fetch_array($data);

    echo(json_encode($row));
?>

据推测,json_decode不是正确的路线,但是我很难理解替代方案会是什么。

3 个答案:

答案 0 :(得分:2)

您的JS部分需要更新

替换此块:

var $username = USERNAME;
var $array = array(1, 2, 3, 4, 5);

使用:

var username = 'USERNAME';
var values = [1, 2, 3, 4, 5];

对于PHP部分尝试使用此(假设您使用PHP&gt; = 5.3):

<?php

$username = trim($_POST['username']);
$fields = isset($_POST['array']) && is_array($_POST['array']) ? $_POST['array'] : array();

$dbc = mysqli_connect ('HOST', 'USERNAME', 'PASSWORD', 'DATABASE', 3306);

$username = mysqli_real_escape_string($dbc, $username);
$fields = array_map(function ($field) use($dbc) {
    $field = mysqli_real_escape_string($dbc, trim($field));
    return 'authors.'.$field;
}, $fields) ;
$fields = implode(',', $fields);
$query = sprintf("SELECT %s FROM details INNER JOIN auths AS authors USING (code) WHERE un = '%s'", $fields, $username);

$rows = array();
if($result = mysqli_query($dbc, $query)) {
    while($row = mysqli_fetch_assoc($result)) {
        $rows[] = $row;
    }
}

echo json_encode($rows);

注意:您应该应用额外的验证,确保用户名和字段不为空,以及接收错误的字段名称,因为如果发生任何这些错误,查询将抛出错误,并且可能会公开有关您的代码和/或数据库的信息。

答案 1 :(得分:0)

你能试试吗,

        var $username = 'USERNAME';
        var $array = new Array(1, 2, 3, 4, 5);


        $.ajax ({
            cache: false,
            url: PHP_FILE,
            dataType: "json",
            type: "post",
            data: {username: $username, array: $array},
            success: function(data) {
                console.log(data);
            }
        });

答案 2 :(得分:0)

JQuery

        var username = USERNAME;
        var array = new Array(1, 2, 3, 4, 5);


        $.ajax ({
            cache: false,
            url: PHP_FILE,
            dataType: "json",
            type: "post",
            data: {username: $username, array:  JSON.stringify(array)},
            success: function(data) {
                console.log(data);
            }
        });

PHP

<?php
    $dbc = DATABASE CONNECTION;

    $username = mysqli_real_escape_string($dbc, trim($_POST['username']));
    $auth = mysqli_real_escape_string($dbc, trim($_POST['array']));

    $array = json_decode($auth, true);

    $query = "SELECT auths.".$array." FROM details INNER JOIN auths USING (code) WHERE un = '".$username."'";
    $data = mysqli_query($dbc, $query);
    $row = mysqli_fetch_array($data);

    echo(json_encode($row));
?>