javascript - 使用 PHP 将 JS 嵌套数组从 localStorage 保存到 MySQL 表
问题描述
我正在将数组推送到 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);
});
}
保存注释.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 数据库和表的连接非常完美。当我硬编码每个字段的值时,数据会更新到表中。我认为问题在于将数组从 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);
推荐阅读
- android - 访问附近状态;反应原生
- python - 我正在学习scrapy,它测试没有错误,但没有抓取任何东西
- react-native - TypeError: undefined is not an object (evaluate 'e.default') in release mode
- python - 使用python访问googlesheet单元格中存在的图像?
- python - 如何水平合并两个列表
- javascript - 通过标志显示开关
- php - 更新 - 两个表之间的链接以获取用户名 - mysql / php
- r - 如何在其他正态分布中处理零条目
- python - 如何为安卓制作一个基于硒的自动化应用程序?
- node.js - 使用 connect-redis 时,出现此错误:“TypeError: Cannot set property 'user' of undefined”