繁体   English   中英

使用php mysqli插入json数组时出错

[英]Error while inserting a json array using php mysqli

我有一个数组作为json.stringify对象。 该数组来自html表。 我做了阵列,它做得很好。 我使用下面的代码创建数组,并尝试获取要插入到我的数据库中的数组。 我在那里使用ajax调用功能

 $(document).on('click','#display_data',function(e){ var convertTableToJson = function() { var rows = []; $('.table-bordered tr').each(function(i, n){ var $row = $(n); rows.push([ $row.find('td:eq(0)').text(), $row.find('td:eq(1)').text(), $row.find('td:eq(2)').text(), $row.find('td:eq(3)').text(), $row.find('td:eq(4)').text(), $row.find('td:eq(5)').text(), $row.find('td:eq(6)').text(), $row.find('td:eq(7)').text(), ]); }); return JSON.stringify(rows); }; $.ajax({ data:convertTableToJson, type:"POST", url:"../php/tagihan/save_data.php", success: function(data){ alert ("Data:" + data); } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.3/jquery.min.js"></script> <table class="table table-bordered"> <thead> <tr> <th>Kode Material</th> <th>Storage Location</th> <th>Movement Type</th> <th>Id Indentifier</th> <th>Item</th> <th>Date Input</th> <th>Netto</th> <th>Unit</th> <th><input type="checkbox" id="mycheckbox1" name="mycheckbox1"></th> </tr> </thead> <tbody> <tr> <td>101200</td> <td>WCB</td> <td>101</td> <td>5006540050</td> <td>1</td> <td>10.08.2017</td> <td>23.970</td> <td>KG</td> <td><input type="checkbox" id="mycheckbox" name="mycheckbox"></td> </tr> <tr> <td>101200</td> <td>WCB</td> <td>101</td> <td>5006539985</td> <td>1</td> <td>10.08.2017</td> <td>42.970</td> <td>KG</td> <td><input type="checkbox" id="mycheckbox" name="mycheckbox"></td> </tr> </tbody> </table> <input type="button" id="display_data" name="display_data" /> 

我不知道为什么。 当我尝试使用此php函数运行此代码时,出现错误提示invalid argument of foreach()invalid argument of foreach() 而且我认为我使用的foreach语法对我用于插入数据的php函数是正确的,就像下面的代码一样

 <?php include('../../Connections/koneksi.php'); // reading json file $array = json_decode($_POST['convertTableToJson']); $data = json_decode($array, true); foreach ($data as $user) { $kode = $user[0]; $sloc = $user[1]; $move = $user[2]; $id = $user[3]; $date = $user[4]; $netto = $user[5]; $unit = $user[6]; $payroll = $user[7]; // preparing statement for insert query $st = mysqli_prepare($db, 'INSERT INTO wjm(kode, sloc, move, id, date, netto, unit, payroll) VALUES (?, ?, ?, ?, ?, ?, ?, ?)'); // bind variables to insert query params mysqli_stmt_bind_param($st, 'ssssssss', $kode, $sloc, $move, $id, $date, $netto, $unit, $payroll); // executing insert query mysqli_stmt_execute($st); } ?> 

请有人帮我解决这个问题。

如果要获取foreach的值,则必须在foreach中获取键值

foreach ($data as $key => $value) {
    $kode       = $value[0];
    $sloc       = $value[1];
    $move       = $value[2];
    $id         = $value[3];
    $date       = $value[4];
    $netto      = $value[5];
    $unit       = $value[6];
    $payroll    = $value[7];

    ...
}

更多信息: http : //php.net/manual/en/control-structures.foreach.php

您确定$array = json_decode($_POST['convertTableToJson']); 得到数组对象?

使用@Raul代码修复语法错误后,您将遇到实用错误。 Is应该在for循环中使用if / else语句编写。 因此,您需要一个for循环来避免重复,就像@Raul代码一样。

for ($x = 0; $x < count($data); $x++) {
    if($x == 0) {
        $kode = $data[0];
    } else if($x == 1) {
        $sloc = $data[1];
    } else if($x == 2) {
        $move = $data[2];
    } else if($x == 3) {
        $id = $data[3];
    } else if($x == 4) {
        $date = $data[4];
    } else if($x == 5) {
        $netto = $data[5];
    } else if($x == 6) {
        $unit = $data[6];
    } else if($x == 7) {
        $payroll = $data[7];
    } else { // do nothing }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM