|
Posted by Jerry Stuckle on 04/05/07 18:31
Jerim79 wrote:
> A new situation that has arised is that now the for loop, even though
> it doesn't insert anything other than 0 to the database, it seems to
> be stuck in an infinite loop. Here is the code:
>
> <?php
>
> if ($_SERVER["REQUEST_METHOD"] == "POST") {
> $Number=$_POST['Number'];
> echo $Number;
> $Age[]=$_POST['Age'];
$Age = $_POST['Age'];
> $Height[]=$_POST['Height'];
$Height = $_POST['Height'];
>
> include ("../functions/db_conn.php");
>
> for ($i=0; i <= $Number; $i++) {
for ($i=0; $i <= $Number; $i++) {
> $query="INSERT INTO Age(Age,Height)
> VALUES('$Age[$i]','$Height[$i]')";
> echo $Age[i], $Height[i];
> $result = mysql_query($query) or die('Query failed: ' .
> mysql_error());
> }
>
> There are two echo $Number commands, one is not pictured here as it is
> at the bottom of the page. Both of these confirm that $Number equals
> 2. I might try a while loop, to just see if that fixes anything. I
> don't know why this infinite loop just appeared.
>
I made some corrections above.
$_POST['Age'] is already an array. When you put it into $Age[], you're
putting the entire array into the first element of another array - $Age.
You want to just put it into $Age. The same with $Height.
And the problem with your loop is you were missing a '$' in front of the
second 'i'.
In a development environment you should always have all errors on and
displayed (error_reporting = E_ALL and display_errors = On in your
php.ini file). You would have gotten a notice about 'i'. And if you
hadn't already created $Age and $Height as arrays, you would have gotten
messages there, also.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
[Back to original message]
|