I have been racking my brain trying to figure this out and I almost have it fully working but I'm running into an issue with the second array $date_start = explode(',', $_POST['date_start']); $date_end = explode(',', $_POST['date_end']); foreach ($date_start as $key) { $SQL = "INSERT INTO mytable(date_start, date_end,) VALUES ('$key', '$date_end')"; $res = mssql_query($SQL); } Code (markup): Each date is delimited by comma's 04/04/13, 04/05/13, etc. and I need it to insert each date as it's own record in the database. It does just that but I can't seem to get the $date_end to tag along with the $date_start. Since it's not part of the foreach statement, it displays array for each date. If I use a second foreach statement, it doubles all entries. Any help would be greatly appreciated. I have always sucked at arrays but it's about time I start learning how to use them better.
I also suck with arrays, but I found something via google, that could help you http://stackoverflow.com/questions/4480803/two-arrays-in-foreach-loop Code (markup):
Not sure if this is what you are trying to do, but this will insert each date_start element with it's corresponding date_end element from the array. $date_start = explode(',', $_POST['date_start']); $date_end = explode(',', $_POST['date_end']); foreach ($date_start as $sub => $key) { $SQL = "INSERT INTO mytable (date_start, date_end) VALUES ('$key', '{$date_end[$sub]}')"; $res = mssql_query($SQL); } Code (markup):
Awesome! That's exactly what I needed. array_combine did the job foreach (array_combine($date_start, $date_end) as $key => $key2) { Code (markup): Thanks so much. Already tested it and it works great.
I'd advise AGAINST array_combine since if you have the same value as more than one date_start, it will erase/trump itself deleting records! that's a REALLY bad approach to it unless you are certain that $date_start in your list does not include the same values more than once. If you know both arrays will always end up the same length, meaning they have the same keys from the explode, you can do this far simpler and in less memory. Of course you screwed up, you're calling the value the key... that's NOT the key. $date_start = explode(',', $_POST['date_start']); $date_end = explode(',', $_POST['date_end']); foreach ($date_start as $key => $startDate) { $SQL = " INSERT INTO mytable ( date_start, date_end ) VALUES ( '$startDate', '{$date_end[$key]}' )"; $res = mssql_query($SQL); } Code (markup): though this would be FAR simpler as PDO. $date_start = explode(',', $_POST['date_start']); $date_end = explode(',', $_POST['date_end']); $statement=$db->prepare(' INSERT INTO mytable( date_start,date_end ) VALUES ( :startDate, :endDate ) '); foreach ($date_start as $key => $startDate) { $statement->execute(array( ':startDate' = $startDate, ':endDate' = $date_end[$key] )); } Code (markup): Since much like mysql_ functions, this is 2013 not 2005... Wondering why they haven't thrown up the big red warning boxes on those too. PDO's big advantage here being all your values would actually be sanitized automatically, something missing from your code and something of a danger, since you're just dumping $_POST values in blindly. array_map may actually be a more useful method of handling it. Something like: $statement=$db->prepare(' INSERT INTO mytable( date_start,date_end ) VALUES ( :startDate, :endDate ) '); function dateExec($startDate,$endDate) { global $statement; $statement->execute(array( ':startDate' = $startDate, ':endDate' = $date_end[$key] )); return null; } array_map( 'dateExec', explode(',', $_POST['date_start']), explode(',', $_POST['date_end']) ); Code (markup): ... that's untested/unproven methodology though. Probably simpler to stick with the foreach and share the index. Also, you should probably add some code to make sure that both arrays being referenced are ACTUALLY the same size... Just to be sure since god knows what's feeding it for postdata.
Thanks deathshadow. I think I now understand what the $key variable is. I'll definitely try your way and see if I can get that to work.