Page 1 of 1

array sql insert problem

Posted: Sun Nov 23, 2003 7:06 pm
by speedamp
hello everyone.


I am creating a signup form where a validation occurs, and during this validation, a user signifies how many entries they would like to add (to a mysql database).

My problem deals with posting the array to the database correctly.....i can not get the syntax correct.

here is my form:

-------------------------------------------------------------------------

if ($submit) {


// process form

$db = mysql_connect("..", ".....", "........");

mysql_select_db(".....",$db);

$sql = "INSERT INTO runner (first,last,age) VALUES ('$first','$last','$age')";

$result = mysql_query($sql);
echo "Thank you for registering your team members. \n"
;


} else{

?>

<table border="2" width="100%"><tr><td>
<form method="post" action="<?php echo $PHP_SELF?>" onSubmit="return Validator(this);">
<table width="100%" border="0">

<?
$row = 5;
for ($i = 0; $i < $row; $i++) {
echo"
<tr><td>
First Name: <input type=\"Text\" name=\"first[$i]\" size=\"10\">
Last Name: <input type=\"Text\" name=\"last[$i]\" size=\"10\">
Age: <input type=\"Text\" name=\"age[$i]\" size=\"5\">
</td></tr>";
}
?>

<input type="Submit" name="submit" value="enter information">
</form>
</table>

<?php

} // end if

?>

----------------------------------------------------------------------------

How do i pass these $rows as individual rows with ONE submit button?


Would it be something to this effect:

foreach($first as $value)
{
???????
}

How would i loop a foreach to include this row array?

Thanks in advance,

-Michael

Posted: Sun Nov 23, 2003 7:35 pm
by infolock
try this :

Code: Select all

<?
$sql = "INSERT INTO runner (first,last,age) VALUES ('".$first."','".$last."','".$age."')"; 
?>
becase you was trying to insert the $first not as a variable, but as a word into your table.

edit :

also
instead of this :

Code: Select all

echo "
First Name: <input type="Text" name="first[$i]" size="10"> 
Last Name: <input type="Text" name="last[$i]" size="10"> 
Age: <input type="Text" name="age[$i]" size="5"> ";
try this

Code: Select all

echo '
First Name: <input type="Text" name="'.first[$i].'" size="10">
Last Name: <input type="Text" name="'.last[$i].'" size="10"> 
Age: <input type="Text" name="'.age[$i].'" size="5"> 
';
noticed how i used a ' instead of a " for the echo statement. makes life much easier when dealing with html calls inside an echo statement..