【问题标题】:Insert Into MySQL from Dynamic Array sent through Ajax从通过 Ajax 发送的动态数组插入 MySQL
【发布时间】:2015-09-16 19:03:58
【问题描述】:

更新的解决方案

我意识到发送这个数组不应该这么复杂。我回去修改了我原来的 jquery 脚本来 push() [] 项。

var seqImg = new Array();
    $('input[name="seqImg[]"]').each( function() {
        seqImg.push($(this).val());
    } );

对于 ajax,我使用了 data: {seqImg:seqImg}async:false 的附加配置,以确保序列保持正确的顺序。

在 PHP 中...

$sql= "INSERT INTO  unit_test(`unit_id`,`seq_img`) VALUES ";            

              $i=0;
              $seqImgs_count = count($seqImgs);

              //$data = $seqImgs_count;
              //echo $data;

              foreach($seqImgs as $item) {

                  $end = ($i == $seqImgs_count-1) ? ';' : ',';

                  $sql .= "('".$unit_id1."','".$item."')".$end;

有效!

原始消息(它的价值...)

我正在尝试收集一组由用户动态生成的序列。

到目前为止,我已经模拟了AustinAllover's answer。这似乎让我很接近,但并不完全在那里。

PHP

// Connect to the database
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
    or die ('Error connecting to MySQL server.');


    if(isset($_POST["result"])){ 
        $json       =   trim($_POST['result']);
        $item_array =   json_decode($json,true);

        // Begin building the query
        // ------------------------
        $sql= "INSERT INTO  unit_test (`unit_id`,`seq_img`,`seq_desc`) VALUES ";            

        $i=0;
        $array_count = count($item_array);

            // ------------------------
            // NOTE: I've echo'd $array_count
            //...it is showing `1`
            //...but it is supposed to be 3
            // ------------------------

        $data = $array_count;
        echo $data;

        foreach($item_array as $item) {

            // Create comma or semi-colon       
            $end = ($i == $array_count) ? ';' : ',';

            // Build each row of data to insert
            $sql .= "('".$item['unit_id']."','".$item['array_seq_image']."','".$item['array_seq_desc']."')".$end;
            $i++;
                }
        mysqli_query($dbc,$sql)
            or die('Error with INSERT. '.$sql);

        mysqli_close($dbc);

小提琴

警报共享通过 Ajax data: {result:JSON.stringify(seq_array)} 发送的 seq_array,我已包含示例 Jquery/Ajax: jsFiddle

结果

Jquery Alert 正在显示发送的数组:

"{"0":{"unit_id":1","ajax_seq_image":"111","ajax_seq_desc":"sample1"},"1":{"unit_id":1","ajax_seq_image":"222","ajax_seq_desc":"sample2"},"2":{"unit_id":1","ajax_seq_image":"333","ajax_seq_desc":"sample3"}}"

控制台向我显示查询尝试的以下结果:

<b>Warning</b>:  Invalid argument supplied for foreach() in <b>/home/cpardonc/public_html/mw/test_form_process2.php</b> on line <b>32</b><br />
Error with INSERT. INSERT INTO  unit_test (`unit_id`,`seq_img`,`seq_desc`) VALUES 

【问题讨论】:

  • 请粘贴 json_decoded $_POST['result']?
  • @Anish 这是trim($_POST['result']的回声:"\"{\"0\":{\"unit_id\":1\",\"ajax_seq_image\":\"111\ ",\"ajax_seq_desc\":\"样本 1\"},\"1\":{\"unit_id\":1\",\"ajax_seq_image\":\"222\",\"ajax_seq_desc\" :\"sample 2\"},\"2\":{\"unit_id\":1\",\"ajax_seq_image\":\"333\",\"ajax_seq_desc\":\"sample 3\" }}\""
    警告:在 /home/cpardonc/public_html/mw/test_form_process2.php 中的 行中为 foreach() 提供的参数无效35
    INSERT 出错。INSERT INTO unit_test (unit_id,seq_img,seq_desc) VALUES
  • json 字符串格式不正确。 seq_array += '"'+seq_order+'"'+':{"unit_id":"'+unit_id+'","'+seq_image+'":"'+si+'","'+seq_desc+'":"'+ sd+'"}'+逗号2;
  • 在您的foreach 中向我们提供echo $sql; exit; 的输出。
  • @Anish 我认为你是对的。我正在尝试创建一个发送以下新行的数组:{"unit_id[0]":1","ajax_seq_image[0]":"111","ajax_seq_desc[0]":"sample1"} {"unit_id[1]":1","ajax_seq_image[1]":"222","ajax_seq_desc[1]":"sample2"} {"unit_id[2]":1","ajax_seq_image[2]":"333","ajax_seq_desc[2]":"sample3"} ...我认为

标签: php jquery ajax


【解决方案1】:

如果您错过了在原始问题开头发布的更新解决方案... 我意识到发送这个数组不应该这么复杂。我回去修改了我原来的 jquery 脚本来 push() [] 项。

var seqImg = new Array();
$('input[name="seqImg[]"]').each( function() {
    seqImg.push($(this).val());
} );

对于 ajax,我使用了 data: {seqImg:seqImg}async:false 的附加配置,以确保序列保持正确的顺序。

在 PHP 中...

$sql= "INSERT INTO  unit_test(`unit_id`,`seq_img`) VALUES ";            

          $i=0;
          $seqImgs_count = count($seqImgs);

          //$data = $seqImgs_count;
          //echo $data;

          foreach($seqImgs as $item) {

              $end = ($i == $seqImgs_count-1) ? ';' : ',';

              $sql .= "('".$unit_id1."','".$item."')".$end;

有效!

【讨论】:

    【解决方案2】:

    您的 json 格式不正确。你错过了一个 " 。

    应该是这样的

    {"0":{"unit_id":"1","ajax_seq_image":"111","ajax_seq_desc":"sample1"},"1":{"unit_id":"1","ajax_seq_image":"222","ajax_seq_desc":"sample2"},"2":{"unit_id":"1","ajax_seq_image":"333","ajax_seq_desc":"sample3"}}
    

    但就是这样。

    {"0":{"unit_id":1","ajax_seq_image":"111","ajax_seq_desc":"sample1"},"1":{"unit_id":1","ajax_seq_image":"222","ajax_seq_desc":"sample2"},"2":{"unit_id":1","ajax_seq_image":"333","ajax_seq_desc":"sample3"}}
    

    看到区别了吗?

    【讨论】:

    • 好收获。修复了丢失的“。仍然收到相同的控制台消息:
    • 更改后 $item_array 的输出是什么? var_dump($item_array) 的输出是什么;
    • var_dump($item_array) 提供了以下内容: string(213) ""{"0":{"unit_id":"1","ajax_seq_image":"111","ajax_seq_desc":"样本 1"},"1":{"unit_id":"1","ajax_seq_image":"222","ajax_seq_desc":"样本 2"},"2":{"unit_id":"1"," ajax_seq_image":"333","ajax_seq_desc":"样本 3"}}""
    • ok 那么 $item_array = json_decode($json,true); 有问题试试这个 $item_array = json_decode(trim($json,"\""),true);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-28
    • 1970-01-01
    • 2013-01-09
    • 2017-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多