以下PHP代碼工作正常時,只有一組數組值在POST,即在索引值0 ...當索引值大於1時,重複的條目被插入到表中..請幫助...在數據庫中批量插入
$sql = "INSERT INTO js (s_name, s_age, s_marks, s_school) VALUES ";
foreach($_POST as $objResult)
{
$i = 0;
foreach($objResult as $Result){
$i++;
if($i>1)// add ',' after first set of values in INSERT..
{
$sql .= ",";
}
$name = $Result['sname'];
$age = $Result['age'];
$mark = $Result['mark'];
$school = $Result['school'];
$sql .= "('".$name."','" .$age."','".$mark."','" .$school."')";
$result=$conn->query($sql);
}}
代碼將無法'$ SQL =「( 「。implode(',',$ Result)。」)「;'做,沒有所有你正在進行的愚蠢行爲? – Darren 2015-02-11 06:21:49
您也可以嘗試設置一個唯一約束作爲快速修復。 – 2015-02-11 06:29:07