php中,Ajax post过来的数组foreach失败的问题。

我在html页面通过ajax传递数据到php,foreach时失败,提示Warning: Invalid argument supplied for foreach() in D:\phpStudy\WWW\test-ht\php\baocunshuju.php on line 10
怎么解决?谢谢。

<?php
header("Content-Type: text/html; charset=utf-8");

$data=$_POST["shuju"]; //这样post数据过来就foreach失败。
//$data=[["1","2017-10-19","11","22","33","44","55","0","0","0","0","0"],["2","2017-10-20","66","77","88","99","1010","0","0","0","0","0"],["3","2017-10-21","211","222","233","244","255","311","322","333","344","355"]]; //这样直接输入数据则能成功。
include("conn.php");

$bcts=0;

foreach ($data as $value){
$sql = "INSERT INTO dashuju (id,riqi,xinpai_db,xinpai_gz,xinpai_hz,xinpai_mn,xinpai_xy,guidang_db,guidang_gz,guidang_hz,guidang_mn,guidang_xy) VALUES ('','$value[1]','$value[2]','$value[3]','$value[4]','$value[5]','$value[6]','$value[7]','$value[8]','$value[9]','$value[10]','$value[11]')";

if($link->query($sql)){$bcts++;}

}

if($bcts>0){
echo "成功保持".$bcts."条";
}else{
echo "保存失败" . $data;
};

?>
最新回答
探春

2024-06-23 02:19:56

没用过PHP,但很明显你POST过来的是个字符串,并不是JSON对象,查了一下似乎是要用json_decode,即类似于$data=json_decode($_POST["shuju"])