dongtuo3370 2018-07-31 08:55
浏览 69
已采纳

使用PHP将JS嵌套数组从localStorage保存到MySQL表

I am pushing arrays to localStorage which I want to store in a MySQL table upon clicking a button.

var coords = JSON.parse(localStorage.getItem('coords'));

the var coords provides me with the following output:

["STAR_SPORTS_2-20170924-200043-210917-00142.jpg", "PerimeterBoard", "Jupiter", 236, 353, 292, 319, 312, 362, "STAR_SPORTS_2-20170924-200043-210917-00142.jpg", "TShirt", "Redshift", 268, 410, 381, 398, 381, 469, "STAR_SPORTS_2-20170924-200043-210917-00142.jpg", "Mic", "Airtel", 317, 327, 425, 329, 421, 371, "STAR_SPORTS_2-20170924-200043-210917-00142.jpg", "Mic", "Airtel", 575, 300, 575, 300, 626, 282]

I am using the following function to push this array to a PHP file:

function dbSave(e) {
  e.preventDefault();
  var coords = JSON.parse(localStorage.getItem('coords'));
   $.post("saveAnnotations.php", {'data' : coords }, function(output){
      alert(output);
      });
}

saveAnnotations.php

<?php
$connect = mysqli_connect($hostname, $username, $password,$database);
$lsData = isset($_POST['data'])?$_POST['data']:'';
$annArray = json_decode($lsData, true);

$image = $annArray[0];
$location = $annArray[1];
$brand = $annArray[2];
$firstX = $annArray[3];
$firstY = $annArray[4];
$secondX = $annArray[5];
$secondY = $annArray[6];
$thirdX = $annArray[7];
$thirdY = $annArray[8];

$sql = "INSERT into `annotations` (`matchName`, `imagename`, `locationName`, `brandname`, `firstx`, `firsty`, `secondx`,`secondy`, `thirdx`,`thirdy`) values 
        ('$matdir','$image', '$location','$brand','$firstX','$firstY','$secondX','$secondY','$thirdX','$thirdY');";

$result = mysqli_query($connect, $sql) or die(mysqli_error($connect));
if ($result) {
    echo "Data uploaded Successfully";
}
$connect->close();

?> 

The connection to the MySQL Db and table is perfect. When I hardcoded the values of each one of the fields, the data gets updated into the table. I think the problem is with posting the array from localStorage to PHP and decoding the same at PHP end.

$annArray = json_decode(json_encode($lsData, true), true);
$countArray = count($annArray);
$interval = $countArray/9;


function fill_chunck($array, $parts) {
    $t = 0;
    $result = array_fill(0, $parts - 1, array());
    $max = ceil(count($array) / $parts);
    foreach($array as $v) {
        count($result[$t]) >= $max and $t ++;
        $result[$t][] = $v;
    }
    return $result;
}

//echo print_r(fill_chunck($annArray, $interval));
$inputArray = [];
$inputArray = fill_chunck($annArray, $interval);

foreach($inputArray as $value=>$data) {
    $image = $data[0];
    $location = $data[1];
    $brand = $data[2];
    $firstX = $data[3];
    $firstY = $data[4];
    $secondX = $data[5];
    $secondY = $data[6];
    $thirdX = $data[7];
    $thirdY = $data[8];

    $sql = "INSERT into `annotations` (`imagename`, `locationName`, `brandname`, `firstx`, `firsty`, `secondx`,`secondy`, `thirdx`,`thirdy`) values 
        ('$image', '$location','$brand','$firstX','$firstY','$secondX','$secondY','$thirdX','$thirdY');";


    $result = mysqli_query($connect, $sql) or die(mysqli_error($connect));
    if ($result) {
        echo "Data uploaded Successfully";
    }
    $connect->close();


}
  • 写回答

1条回答 默认 最新

  • dongtang5229 2018-07-31 09:26
    关注

    Try this one and paste output of $annArray

    $connect = mysqli_connect($hostname, $username, $password,$database);
    $lsData = isset($_POST['data'])?$_POST['data']:'';
    $annArray = json_decode(json_encode($lsData, true), true);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大