Page 1 of 1

MySQL PHP Mulitple insert

Posted: Sat Jan 27, 2007 9:50 pm
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());
}


Posted: Sun Jan 28, 2007 9:04 am
by feyd
Example: name="driverType" :arrow: name="data[10][driverType]"

Posted: Sun Jan 28, 2007 11:58 am
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.

Posted: Mon Jan 29, 2007 3:43 am
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?

Posted: Tue Jan 30, 2007 6:07 am
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>";
    }
}