i have this piece of code which permits me to insert on the database some information:
$att_id=$_REQUEST['att_id'];
$date=$_REQUEST['date'];
$pdv=$_REQUEST['checkbox'];
foreach($date as $val) {
foreach ($pdv as $pdv1) {
$values_arr[] = "('" . $val . "','" . $pdv1 . "','" . $att_id . "')";
$values = implode(", ", $values_arr);
$sql="INSERT INTO date_pdv (date,id_pdv,att_id) VALUES $values";
mysql_query($sql)or die(mysql_error());
}
}
Now, the points is that if array date has this values [1,2] and array pdv has this ones [1,2,3] what i should get in the database is:
1-1
1-2
1-3
2-1
2-2
2-3
But i don’t… i get repeated values on the database… Should i be using any while length of the array..etc etc?
Thanks!
You were running the insert query on each iteration of the loop, which is why you were ending up with a lot of duplicated rows.
Note the placement of the closing
}on theforeachloops