您好我正在尝试将动态数组从 jQuery 发布到 PHP 到 mysql 我从 jQuery 获取数据并能够序列化数据 php 获取变量并且它作为数组出现但是当我尝试为我的插入和依赖传递变量时我把它放在 foreach() 我要么只得到插入数据库的最后一个数组,要么得到所有东西的多个插入(即:name1、prob1、date1 name1、prob1、date2 等)。$name, $problem, $timedate 作为 jQuery 的数组发布。
在这个例子中,我只得到数组的最后一个
<?php
$name = $_POST['name'];
$problem = $_POST['problem'];
$timedate = $_POST['timedate'];
$con = mysql_connect("localhost","wayko","b4v0e1jj");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("joinus", $con);
$seriname = mysql_real_escape_string(serialize($name));
$seriprob = mysql_real_escape_string(serialize($problem));
$seritd = mysql_real_escape_string(serialize($timedate));
foreach($name as $valname){
foreach($problem as $valprob){
foreach($timedate as $valtd){
}
}
}
$sql="INSERT INTO roomchart (Name,TimeDate,Problem)
VALUES
('$valname','$valtd','$valprob')";
echo $sql;
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error(). "Actual query: " . $sql);
}
mysql_close($con);
?>
在此示例中,我获得了相同数据的多个插入,因此 3 个插入变为 9
foreach($name as $valname){
foreach($problem as $valprob){
foreach($timedate as $valtd){
$sql="INSERT INTO roomchart (Name,TimeDate,Problem)
VALUES
('$valname','$valtd','$valprob')";
echo $sql;
}
}
}
有任何想法吗?