Jump to content

Insert Multi Array into MySQL


l!m!t
 Share

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
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.

Link to comment
Share on other sites

 

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?

 

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • 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.