MPAA - Myspace Comments - Loans - Loans - Eyes on Me

PDA

View Full Version : How to insert Multiple Queries?


lost
Oct 25th 2005, 9:53 am
I have something a little complex here, i hope that i can explain it correctly.
I have a form with 4 textfields to start with. However, it gets dynamically created larger if the user hits the tab key in the last field, it will create a new row of textfields.
See code:

<TABLE BORDER="0" CELLSPACING="0" CELLPADDING="0">
<TR>
<TD>Item 1</TD>
</TR>
<TR>
<TD ID="textfields">Description <INPUT TYPE="text" NAME="description" VALUE="" Part Number <INPUT TYPE="text" NAME="partno" VALUE"" ONKEYDOWN="if (event.keyCode==9) { addNewItem(); }"></TD>
</TR>

<TABLE BORDER="0" CELLSPACING="0" CELLPADDING="0">
<TR>
<TD>Wiring Harness 1</TD>
</TR>
<TR>
<TD ID="whtextfields">Description <INPUT TYPE="text" NAME="description" VALUE="" Part Number <INPUT TYPE="text" NAME="partno" VALUE="" ONKEYDOWN="if (event.keyCode==9) { addNewWiringHarness(); }"></TD>
</TR>
</TABLE>


Now, at the end of the form there is a cancel and save button. If the save button is pressed, all the data entered in the fields gets inserted into a mysql database.
Currently, i only know how to insert 2 fields, not a indefinite number.

This is how i'm doing it:
<?php
//if 'Submit' button was pressed
if(isset($_POST['savebutton']))
{
$message = NULL;

if(ereg("[([0-9a-zA-Z_]|\-|\[|\])", $_POST['description']))
{
//valid description
$desc = $_POST['description'];
}
else
{
//invalid description
$desc = FALSE;
$message .= '<p>Description not valid.<p>';
}


if((ereg("[0-9]{6}", $_POST['partno'])) && (strlen($_POST['partno']) >= 5))
{
//valid part number
$pno = $_POST['partno'];
}
else
{
//invalid part no
$pno = FALSE;
$message .= '<P>Part No must be in the format: 999999<P>';
}

if ($desc && $pno)
{
require_once('./mysql_connect.php');

$query = "INSERT INTO systemconfig_generic(description, partno) VALUES ('$desc', '$pno')";
$result = @mysql_query($query);

if($result)
{
echo '<FONT SIZE=2>The following was added to the AWACS lab database:<br>';
echo 'Description: ' .$desc .'<br>';
echo 'Part No: ' .$pno;
}
else
{
$message = '<P><FONT SIZE=2>The following sql error occured:</P><P>' .mysql_error() .'</P><FONT>';
echo $message;
}
mysql_close();
}


Any ideas would be great please.

clickbuild
Oct 29th 2005, 12:13 pm
The trick is to bang the rocks together -HHGTTG

Actually the trick is that the form field in the HTML example above should be NAME="description[]" -- PHP will turn this into an array which will then be available in $_POST[description] -- change the HTML above and add print_r($_POST) to your code to see what is returned from the form when you submit it.