$job_id=$_REQUEST['job_id'];
$dates = explode(",", $_POST['altField']);
foreach($dates as $date){
$values .= '("'.$job_id.'","'.$date.'"),';
}
$sql="INSERT INTO date (job_id,date) VALUES " . substr($values, 0, -1);
mysql_query($sql);
插入可以有多个值集,因此您只需要在 foreach 中设置值,然后在它之外进行查询。您也可以这样做并获得相同的结果,
$job_id=$_REQUEST['job_id'];
$dates = explode(",", $_POST['altField']);
foreach($dates as $date){
$values[] = '("'.$job_id.'","'.$date.'")';
}
$sql="INSERT INTO date (job_id,date) VALUES " . implode(',',$values);
mysql_query($sql);
提醒
始终清理您的输入。不这样做可能会导致 SQL 注入并给您带来重大问题。看这个 还有mysql没有维护,建议改用MySqli或者PDO。