簡體   English   中英

使用PHP將JS嵌套數組從localStorage保存到MySQL表

[英]Saving JS nested array from localStorage to MySQL table using PHP

我將數組推送到localStorage,單擊按鈕后要將其存儲在MySQL表中。

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

var coords為我提供以下輸出:

["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]

我正在使用以下函數將該數組推送到PHP文件:

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

?> 

與MySQL Db和表的連接非常完美。 當我對每個字段的值進行硬編碼時,數據將更新到表中。 我認為問題在於將數組從localStorage發布到PHP並在PHP端對其進行解碼。

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


}

試試這個,粘貼$ annArray的輸出

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM