$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);
Insertions can have multiple sets of values, so you just need to set the values ββin foreach, and then outside it, execute the query. you can also do it this way and get the same result
$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);
REMINDER
ALWAYS sanitize your entrances. Failure to do so could result in SQL injection and cause serious problems for you. Take a look at this. Also mysql is not supported and it is suggested to switch to MySqli or PDO.
source share