i think this query could be optimized by putting all the inserts in one query. something like this:
$query = 'INSERT INTO test (test1, test2) VALUES '; foreach($_POST['test1'] as $i => $value ) { $query .= "('".$_POST['test1'][$i]."', '".$_POST['test2'][$i]."'), "; } $result = mysql_query( substr( $query, 0, -2 ) ); from the MySQL documents on optimizing inserts: "If you are inserting many rows from the same client at the same time, use multiple value lists INSERT statements. This is much faster (many times in some cases) than using separate INSERT statements. " or you could lock the table before the inserts and unlock it after... ------------------ On Sun, 15 Jun 2003 22:13:13 +0200, Frank Keessen wrote (in message <[EMAIL PROTECTED]>): > Hi, > > Wow! Live = beautiful (or is it PHP??).. > > Any way: > > foreach($_POST['test1'] as $i => $Value) { >> $query = "INSERT INTO test (test1, test2) VALUES >> ('".$_POST['test1'][$i]."', '".$_POST['test2'][$i]."')"; >> $result = mysql_query($query); > > That one did the trick! > > Thanks James and Wouter for helping me out! > > Frank > ----- Original Message ----- > From: "esctoday.com | Wouter van Vliet" <[EMAIL PROTECTED]> > To: "Frank Keessen" <[EMAIL PROTECTED]>; "James Hicks" > <[EMAIL PROTECTED]> > Cc: <[EMAIL PROTECTED]> > Sent: Sunday, June 15, 2003 10:08 PM > Subject: RE: [PHP] Capturing the input of dynamic form fields (part II) > > >> First thing I notice as being at least a bit odd is this part: >> >> $nArraySize = count($_POST['test1']); >> $test1=$_POST['test1[]']; >> $test2=$_POST['test2[]']; >> >> what are you trying to do with that? .. What I'd expect it to look like is >> smth like: >> >> $nArraySize = count($_POST['test1']); >> $test1 = $_POST['test1']; >> $test2 = $_POST['test1']; >> >> but probably this: >> >> foreach($_POST['test1'] as $i => $Value) { >> $query = "INSERT INTO test (test1, test2) VALUES >> ('".$_POST['test1'][$i]."', '".$_POST['test2'][$i]."')"; >> $result = mysql_query($query); >> >> # and what else you'd want to do ... >> }; >> >> would just make even a little more sense .. some discussion could be about >> using $_POST['test1'][$i] to get the value again, or just using the $Value >> variable created by the foreach calls... >> >> -----Oorspronkelijk bericht----- >> Van: Frank Keessen [mailto:[EMAIL PROTECTED] >> Verzonden: zondag 15 juni 2003 21:44 >> Aan: Frank Keessen; James Hicks >> CC: [EMAIL PROTECTED] >> Onderwerp: Re: [PHP] Capturing the input of dynamic form fields (part >> II) >> >> >> Hi, >> >> Looks like the value's are not coming to the database.. Can you please all >> have a close look at my code because it's driving me NUTS! >> >> THANKS FOR YOUR HELP! >> >> <? >> // open database connection >> $connection = mysql_connect($host, $user, $pass) or die ("Unable to >> connect!"); >> >> // select database >> mysql_select_db($db) or die ("Unable to select database!"); >> >> $_REQUEST["submit"]=isset($_REQUEST["submit"])?$_REQUEST["submit"]:""; >> if($_REQUEST['submit']!="") >> { >> $nArraySize = count($_POST['test1']); >> $test1=$_POST['test1[]']; >> $test2=$_POST['test2[]']; >> >> echo $nArraySize; >> for($i=0;$i<$nArraySize;$i++) { >> $query="INSERT INTO test (test1, test2) VALUES ('$test1[$i]', >> '$test2[$i]')"; >> $result = mysql_query($query); >> echo(mysql_error()."<BR>$query<BR>".mysql_affected_rows($result)); >> } >> >> } >> ?> >> <form name="form1" method=post id=form1 enctype=multipart/form-data >> action=<?=$_SERVER['PHP_SELF']?>> >> <? >> for ($i=1; $i<=2; $i++) >> { >> echo "# $i<input type=\"text\" name=\"test1[$i]\"><br>"; >> echo "# $i<input type=\"text\" name=\"test2[$i]\"><br>"; >> >> } >> >> ?> >> <input type=submit name=submit value=submit> >> </form> >> >> Regards, >> >> Frank >> >> >> ----- Original Message ----- >> From: "Frank Keessen" <[EMAIL PROTECTED]> >> To: "James Hicks" <[EMAIL PROTECTED]> >> Cc: <[EMAIL PROTECTED]> >> Sent: Sunday, June 15, 2003 8:59 PM >> Subject: Re: [PHP] Capturing the input of dynamic form fields (part II) >> >> >>> Hi James, >>> >>> Sorry to say; I've put your code in it but there are no error message's >>> displaying and there is no value inserted in the database... Any clue??? >>> >>> Regards, >>> >>> Frank >>> ----- Original Message ----- >>> From: "James Hicks" <[EMAIL PROTECTED]> >>> To: "Frank Keessen" <[EMAIL PROTECTED]> >>> Cc: <[EMAIL PROTECTED]> >>> Sent: Sunday, June 15, 2003 4:14 PM >>> Subject: Re: [PHP] Capturing the input of dynamic form fields (part II) >>> >>> >>>> Whenever I am having problems with SQL queries I always display the >> query >>> on >>>> the page with all the replaced variables so I can make sure that the >> query >>>> makes sense. If it passes the MySQL parser in my brain and still > doesn't >>>> work, I will copy the displayed query into the command line mysql >> monitor >>> (or >>>> phpmyadmin) and try the command manually to see what happens. >>>> >>>> I also found it easier to solve my SQL problems by not using "or die " >> and >>>> just displaying some information about the problem query. This way I >> know >>>> something is wrong if the third line of the echo is not at least 1. >>>> >>>> $query="INSERT INTO test (test1, test2) VALUES >>>> ('$_POST['test1']','$_POST['test2']')"; >>>> $result = mysql_query($query); >>>> echo(mysql_error()."<BR>$query<BR>".mysql_affected_rows($result)); >>>> >>>> >>>> James >>>> >>>> On Sunday 15 June 2003 09:39 am, Frank Keessen wrote: >>>>> Dear all, >>>>> >>>>> Refering to my first e-mail with the same subject; I've reworked the >>> code >>>>> to this: >>>>> >>>>> But the only problem is; Nothing is written into the mysql database! >> And >>>>> there are no error displayed.. >>>>> >>>>> Can someone have a look at my code? >>>>> >>>>> <? >>>>> > $_REQUEST["submit"]=isset($_REQUEST["submit"])?$_REQUEST["submit"]:""; >>>>> if($_REQUEST['submit']!="") >>>>> { >>>>> for($i=0;$i<count($_POST['test1[]']);$i++) { >>>>> $query="INSERT INTO test (test1, test2) VALUES >>>>> ('$_POST['test1']','$_POST['test2']')"; $result = > mysql_query($query) >> or >>>>> die ("Error in query: $query. " . mysql_error()); } >>>>> } >>>>> ?> >>>>> <form name="form1" method=post id=form1 enctype=multipart/form-data >>>>> action="<? $_SERVER['PHP_SELF']?>"> <? >>>>> for ($i=1; $i<=2; $i++) >>>>> { >>>>> echo "# $i<input type=\"text\" name=\"test1[]\"><br>"; >>>>> echo "# $i<input type=\"text\" name=\"test2[]\"><br>"; >>>>> >>>>> } >>>>> >>>>> ?> >>>>> <input type=submit name=submit value=submit> >>>>> </form> >>>>> >>>>> Thanks very much, >>>>> >>>>> Frank >>>> >>>> >>>> -- >>>> PHP General Mailing List (http://www.php.net/) >>>> To unsubscribe, visit: http://www.php.net/unsub.php >>>> >>> >>> >>> -- >>> PHP General Mailing List (http://www.php.net/) >>> To unsubscribe, visit: http://www.php.net/unsub.php >>> >> >> >> -- >> PHP General Mailing List (http://www.php.net/) >> To unsubscribe, visit: http://www.php.net/unsub.php >> >> >> >> -- >> PHP General Mailing List (http://www.php.net/) >> To unsubscribe, visit: http://www.php.net/unsub.php >> > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php