Jump to content

Insert Multi Array into MySQL


l!m!t

Recommended Posts

 

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 . "'')");
}

Link to comment
https://forums.phpfreaks.com/topic/232349-insert-multi-array-into-mysql/
Share on other sites

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.

 

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?

 

 

 

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

$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);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.