MySQL PHP Mulitple insert

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

MySQL PHP Mulitple insert

Post by facets »

Hi All,

I have a form that 'can' have many rows of input. Can anyone suggest a way to input each 'Section' one after the other?
Until I can work out how to dynamically make new rows appear I will use 10 rows of input.
The code below enters the 'last' row of data 10 times. If I move the 'mysql_query' statement outside of the loop the 'last' row is entered once.

Any pointers?
Ta, Will./

Code: Select all

// Input Section 1 //
echo "<tr><td width=100px valign=top><select name=\"driverType\">\n
<option value=\"delivery\">Delivery</option>\n 
<option value=\"return\">Return</option>\n</select></td>";
echo "<td width=150px>".showProduct()."</td>";
echo "<td width=75px valign=top>
<input type=\"text\" name=\"amount\" size=\"2\"></input></td></tr>";

// Input Section 2 //
echo "<tr><td width=100px valign=top><select name=\"driverType\">\n
<option value=\"delivery\">Delivery</option>\n 
<option value=\"return\">Return</option>\n</select></td>";
echo "<td width=150px>".showProduct()."</td>";
echo "<td width=75px valign=top>
<input type=\"text\" name=\"amount\" size=\"2\"></input></td></tr>";
  
for ($x = 1; $x <= 10; $x++) {
$sql_add_invoice_items = "INSERT INTO invoice_items VALUES('','$prod_name', '$amount', '$invoice_increment', '$price', '$cooked', '$date', '$wholesale', '$gst', '$wholesale_price', '$wholesale_gst')";              
mysql_query($sql_add_invoice_items) or die(mysql_error());
}

User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Example: name="driverType" :arrow: name="data[10][driverType]"
User avatar
boo_lolly
Forum Contributor
Posts: 154
Joined: Tue Nov 14, 2006 5:04 pm

Post by boo_lolly »

i think what feyd is saying is put the names and values of each of your input fields into an associative array...

Code: Select all

$array = array('name1' => 'value1', 'name2' => 'value2', ...... ect);

then use a foreach() loop to print them dynamically.

Code: Select all

foreach($array as $key => $val){
        echo "<input type=\"text\" name-\"". $key ."\" value=\"". $val ."\">"\n";
}
or something of that nature. i believe feyd intended for you to use a multidimensional array.
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Post by facets »

I think I understand the concept for creating the form dynamically now, thanks.
But I'm still unsure on how to INSERT each of the "rows" into the database.
Can you help?
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Post by facets »

Ok I think I've got a rough idea on the multidimensional array. Although this displays the same output 3 times.
If I remove the 1st foreach loop I get "Multidimensional Array" then the output from the 2nd foreach (which looks fine)

Any ideas on this? Should this be a standard array and not multi?

Code: Select all

echo "<hr>Multidimensional Array";
$cds = array(
  'Christina Aguilera'=>array('Impossible','Beautiful','Dirty'),
  'Pink'=>array('Just like a pill','Family potrait','Missundaztood'),
  'Kelly Rowland'=>array('Stole','Obsession','Past 12')
);

foreach( $cds as $singer=>$songs ) {
    echo "INSERT INTO returns_table VALUES ";
    foreach( $cds as $singer=>$songs ) {
        echo "(" .$songs[0] . ", " . $songs[1] . ", " . $songs[2] . ")\n<br>";
    }
}
Post Reply