duanhui7329 2017-05-20 09:12
浏览 349

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

Actually I have created an array that contains table rows value and I want to pass the array to PHP file using jQuery and I want to store it in MySql database.

I have done as:

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

The above code that will create, store and pass the array to .php file

$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();

The above code will receive the array and try to store in databse. But I am getting an error in foreach statement as: Invalid argument supplied foreach

I searched and come to know that the receiving value is not an array. How to solve the issue???

  • 写回答

3条回答 默认 最新

  • douping7105 2017-05-20 09:22
    关注

    just do

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

    now you can use $data in foreach loop Update 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);
    }
    

    In mysqli_stmt_bind_param please use following types

    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

    for more refernce check this http://php.net/manual/en/mysqli-stmt.bind-param.php

    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况