for loop in forms
| From: | Brett C. | Date: | Mon, 10 Jul 2000 21:01:25 +0000 |
| Subject: | for loop in forms | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-5808@lists.php.net to get a copy of this message | ||
I use a for loop to create form inputs. The code works to add reminders on
one of my pages.
I used the exact same code on another one of my pages, yet only one row gets
inputed every time. I am lost as to the problem. Below is the code that
works, and the code that doesn't.
*Working code:
<? for ($i = 1; $i <= $fields; $i++) : ?>
then select boxes too long to list
<? endfor ?>
*working code to execute:
$numrows = count ($user);
for ($i = 0; $i < $numrows; ++$i) {
$sql = "INSERT INTO reminder (date, time, reminder, user) " .
"Values ('$dm[$i] $dd[$i] $dy[$i]','$th[$i]:$tm[$i]
$ap[$i]','$rem[$i]','$user[$i]')";
if (!mysql_query($sql, $link)) {
DisplayErrMsg(sprintf("Error in executing %s stmt", $sql)) ;
DisplayErrMsg(sprintf("error:%d %s", mysql_errno($link),
mysql_error($link))) ;
exit() ;
*NON-Working Code:
<?
for ($i = 1; $i <= $names; $i++):
?>
<input type="text" name="type[]">
<input type="text" name="date[]">
<input type="text" name="doctor[]">
<input type="hidden" name="user[]" value="<? echo $user;
?>">
<br><?
endfor
?>
*NON-Working code to execute:
$numrows = count ($user);
for ($i = 0; $i < $numrows; ++$i) {
$sql1 = "INSERT INTO childimu (user, type, date, doctor) " .
"Values ('$user7[$i]', '$type[$i]', '$date[$i]',
'$doctor[$i]')";
if (!mysql_query($sql1, $link)) {
DisplayErrMsg(sprintf("Error in executing %s stmt", $sql1)) ;
DisplayErrMsg(sprintf("error:%d %s", mysql_errno($link),
mysql_error($link))) ;
exit() ;
Again the second code only inputs one row into the database(mysql), while
the first code will inout every textbox of the array. I copied and used the
same code for the second. Any suggestions please!!