l!m!t Posted March 31, 2011 Share Posted March 31, 2011 Hello, I have been at this for awhile now and cant seem to figure it out. I have 2 separate arrays like shown below and I am trying to insert each value into my database using one update query. I have tried a for loop inside each array, but only one column populates. I have tried breaking the values outside their for loop, etc, etc. Does anyone know a way this can be done? //Cars $cars = array('ford', 'toyota', 'bmw'); //Places $places =arrray('Orlando','California','Texas'); foreach ($cars as $car){ foreach ($places as $place){ $place .=$place; } mysql_query("INSERT INTO vehicles (cars, places) VALUES ('" . $car. "', '" . $place . "'')"); } Quote Link to comment Share on other sites More sharing options...
cssfreakie Posted April 1, 2011 Share Posted April 1, 2011 this is what i would do to test. place this at the top of your script: <?php error_reporting(E_ALL); ini_set("display_errors", 1); ?> $car='monkey'; $place='atlantis'; $query = "INSERT INTO vehicles (cars, places) VALUES ('$car', '$place')"; echo $query; mysql_query($query) or die(mysql_errno(). mysql_error())); I often do it like that to makes sure the query looks like it should, in the end strip all the redundant parts. Quote Link to comment Share on other sites More sharing options...
l!m!t Posted April 1, 2011 Author Share Posted April 1, 2011 Hi, Thanks for the reply. Well, I know the query works I am stuck on how to insert values from both arrays in one loop. Basically I am trying to save time by using PHP to insert the array values from a for loop. I can get it to insert a single set of array values, however its the second array doesn't input correctly. I somehow have to combine both arrays into one and then have a way to output them as individual values based on their array name. I am not sure if this is even possible. I guess something like this .. $cars = array('ford', 'toyota', 'bmw'); $places =arrray('Orlando','California','Texas'); foreach ($combined_array as $values){ $car=$cars['car']['value']; $place=$places['place']['value']; $query = "INSERT INTO vehicles (cars, places) VALUES ('$car', '$place')"; } Any ideas? Quote Link to comment Share on other sites More sharing options...
cssfreakie Posted April 1, 2011 Share Posted April 1, 2011 ah okay, well i think the following should work $query = "INSERT INTO vehicles (cars, places) VALUES ('$car[0]', '$place[0]'),('$car[1]', '$place[1]'),('$car[2]', '$place[2]')"; if that doesn't work try $query = "INSERT INTO vehicles (cars, places) VALUES ('{$car[0]}', '{$place[0]}'),('{$car[1]}', '{$place[1]}'),('{$car[2]}', '{$place[2]}')"; notice I named it car and place instead of cars and places i saw it after posting Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 1, 2011 Share Posted April 1, 2011 $cars = array('ford', 'toyota', 'bmw'); $places =arrray('Orlando','California','Texas'); $combined_array = array_combine($cars, $places); $values = array(); foreach ($combined_array as $car => $place) { $values[] = "('$car', '$place')"; } $query = "INSERT INTO vehicles (cars, places) VALUES " . implode(', ', $values); Quote Link to comment Share on other sites More sharing options...
cssfreakie Posted April 1, 2011 Share Posted April 1, 2011 woo that's nice, I still have to learn a lot on php Thanks mjdamato! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.