【问题标题】:Error while inserting a json array using php mysqli使用 php mysqli 插入 json 数组时出错
【发布时间】:2017-08-19 13:18:14
【问题描述】:

我有一个数组作为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()。我认为我使用的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);
}

?>

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

【问题讨论】:

  • 你使用了 json_decode 两次;这可能是你的问题

标签: javascript php arrays json mysqli


【解决方案1】:

如果要获取foreach的值,需要在foreach中获取key值

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

【讨论】:

    【解决方案2】:

    你确定 $array = json_decode($_POST['convertTableToJson']); 获取数组对象?

    【讨论】:

    • 使用 cmets 请求澄清。答案是……很好的答案。
    【解决方案3】:

    使用@Raul 代码修复语法错误后,您将遇到语用错误。应该在 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 }
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-04
      • 1970-01-01
      • 1970-01-01
      • 2013-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-17
      • 1970-01-01
      相关资源
      最近更新 更多