如何在PHP中获取JSON数组并打印其值?

时间:2017-05-20 09:12:01

标签: php jquery mysql arrays json

实际上我创建了一个包含表行值的数组,我想使用jQuery将数组传递给PHP文件,我想将它存储在MySql数据库中。

我做了:

var myTableArray = [];

$("table#tbl-nitem tr").each(function() {
    var arrayOfThisRow = [];
    var tableData = $(this).find('td');
    if (tableData.length > 0) {
        tableData.each(function() {
            arrayOfThisRow.push($(this).text());
        });

        alert(arrayOfThisRow);
        arrayOfThisRow.shift();
        myTableArray.push(arrayOfThisRow);
    }
});
//myTableArray.shift();
alert(myTableArray);

var jsonString = JSON.stringify(myTableArray);
$.ajax({
    type: "GET",
    url: "script_expenses.php",
    data: {
        data: jsonString
    },
    cache: false,

    success: function(response) {
        if (response == "ok") {
            alert("OK");
        } else {
            alert(response);
        }
    }
});

以上代码将创建,存储并将数组传递给.php文件

$conn = require_once 'db_connection.php';
//echo $conn;
session_start();
$data = $_GET['data'];
json_encode($data);

//  $sql = "INSERT INTO my_table ( fname, lname ) VALUES ( ?,? )"; 
//$ins = 

$sql = "INSERT INTO expenses_list VALUES (?, ?, ?, ?, ?, ?)";
$stmt = mysqli_prepare($conn, $sql);

foreach($data as $row) {
    mysqli_stmt_bind_param('ss', 'TEA');
    mysqli_stmt_bind_param('ss', $row['Purpose']);
    mysqli_stmt_bind_param('ss', $row['UnitPrice']);
    mysqli_stmt_bind_param('ss', $row['Qty']);
    mysqli_stmt_bind_param('ss', $row['DTime']);
    mysqli_stmt_bind_param('ss', $row['Description']);
    $result = mysqli_stmt_execute($stmt);
}

flush();

上面的代码将接收数组并尝试存储在数据库中。 但我在foreach声明中收到错误: foreach提供的参数无效

我搜索并了解接收值不是数组。 如何解决问题???

3 个答案:

答案 0 :(得分:0)

只是做

$data = $_GET['data'];

$data = json_decode($data,true);

现在你可以在foreach循环中使用$ data 更新1

$conn =  require_once'db_connection.php';
session_start();
$data = $_GET['data'];
$data = json_decode($data,true);

$sql    = "INSERT INTO expenses_list VALUES (?, ?, ?, ?, ?, ?)";
$stmt   = mysqli_prepare($conn,$sql);

foreach($data as $row){
    mysqli_stmt_bind_param('s', 'TEA');
    mysqli_stmt_bind_param('s', $row['Purpose']);

    //mysqli_stmt_bind_param('s', $row['UnitPrice']);
    //if UnitPrice is double values then use
    mysqli_stmt_bind_param('d', $row['UnitPrice']);

    //mysqli_stmt_bind_param('s', $row['Qty']);
    //if Qty is integer then use
    mysqli_stmt_bind_param('i', $row['Qty']);
    mysqli_stmt_bind_param('s', $row['DTime']);
    mysqli_stmt_bind_param('s', $row['Description']);
    $result = mysqli_stmt_execute($stmt);
}

在mysqli_stmt_bind_param中,请使用以下类型

我对应的变量有整数

d对应变量的类型为double

对应的变量具有类型字符串

b对应的变量是一个blob,将以数据包的形式发送

有关详情,请查看此http://php.net/manual/en/mysqli-stmt.bind-param.php

答案 1 :(得分:0)

使用 json_decode()设置第二个参数 true 当为TRUE时,返回的对象将被转换为关联数组。

$data = json_decode($_GET['data'],true);

注意:阅读您的代码有很多错误prepared statement

的文档

1)变量的数量和字符串类型的长度必须与语句中的参数匹配。你为每个变量添加了两个 ss

mysqli_stmt_bind_param('s', $row['Purpose']); //remove one s because your binding one variable only 

2)将变量绑定到预准备语句,因为参数不接受 string

$tea ='TEA'; mysqli_stmt_bind_param('s', $tea);

应该通过引用bind参数传递

变量。不接受字符串。

3)mysqli_stmt_bind_param第一个参数是 $ stmt 。你错过了。加上它。

4)(?)个问号的数量与数字绑定变量匹配,否则会产生错误。

5)

i   corresponding variable has type integer

d   corresponding variable has type double

s   corresponding variable has type string

b   corresponding variable is a blob and will be sent in packets

更新1:

        $tea ='TEA';
    foreach($data as $row){
        mysqli_stmt_bind_param($stmt,'s', $tea); // s denotes   corresponding variable has type string
        mysqli_stmt_bind_param($stmt,'s', $row['Purpose']);
        mysqli_stmt_bind_param($stmt,'s', $row['UnitPrice']);
        mysqli_stmt_bind_param($stmt,'s', $row['Qty']);
        mysqli_stmt_bind_param($stmt,'s', $row['DTime']);
        mysqli_stmt_bind_param($stmt,'s', $row['Description']);
        $result = mysqli_stmt_execute($stmt);
    }

答案 2 :(得分:0)

将json转换为数组。使用以下代码

$datajson = $_GET['data'];
$dataarray = json_decode($datajson, true);