hi, can i use && in foreach loop for example - foreach ($_POST['day'] as $day && $_POST['event'] as $event) { $query = "INSERT INTO event(year,day,event) VALUES('$_POST[year]','$day','$event')"; } PHP: but it is giving error Parse error: syntax error, unexpected T_BOOLEAN_AND, expecting ')' Code (markup): if it is incorrect tell me the right code please. thanks
Assuming that $_POST['day'] and $_POST['event'] are arrays use the code below... <?php $_POST['day'] = array('S','M','T','W','T','F','S'); $_POST['event'] = array('ev1','ev2','ev3','ev4','ev5','ev6','ev7'); $_POST['year'] = 2010; foreach($_POST['day'] as $key => $day) { foreach($_POST['event'] as $event) { $query = "INSERT INTO event(year,day,event) VALUES('$_POST[year]','$day','$event')"; print_r($query); echo "\n"; } } ?> Code (markup): if they are not arrays then using of foreach is useless .. you can just use the code below: <?php $_POST['day'] = 'Monday'; $_POST['event'] = 'some event'; $_POST['year'] = 2010; $query = "INSERT INTO event(year,day,event) VALUES ('$_POST[year]','$_POST[day]','$_POST[event]')"; print_r($query); echo "\n"; ?> Code (markup): I hope this helps
you can do it like this: foreach ($_POST['day'] as $day) { $event=$_POST['event']; $query = "INSERT INTO event(year,day,event) VALUES('$_POST[year]','$day','$event')"; } or if you have multiple events like this: you can do it like this: $i=0; foreach ($_POST['day'] as $day) { $event=$_POST['event'][$i]; $query = "INSERT INTO event(year,day,event) VALUES('$_POST[year]','$day','$event')"; $i++; }