Generating forms in a loop and inserting into the database
Posted: Thu Mar 10, 2005 9:12 am
I'm building a music website for university. I ask the user how many members are in their band and how many bands have influenced them and then generate the amount of forms they need to enter their members and their influences using two seperate loops. That bit works, however I also have to insert into the database the data submitted into the forms in 2 loops using the same while condition. I'm having problems though, here's the code:
I've marked in where it has the problem.
Another thing is that you may notice that I've used an array for the text field name on the member loops and just added $n to the end of the text field name on the influence loops. I wasn't sure if I could do the second thing so I was wondering firstly if the non array method will work and if it will work which method would work best and then I'll change them both to that method.
Any help would be greatly appreciated.
feyd | Please use [php] and
Code: Select all
<?php
session_start();
include('connect.inc');
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Welcome to DIYMusic.com</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#000000" text="#CCCCCC" link="#666666" vlink="#666666">
<table width="96%" height="2000" border="0">
<tr>
<td width="31%" valign="top"><img src="Register%20Side%20Bar.jpg" width="290" height="500" border="0" usemap="#Map"></td>
<div align="center"><b>Enter the details of each member of your band and each band who has influenced you below:</b></div>
<p></p>
<p><p>
<p></p>
<td width="69%" valign="top"><form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<b><i>Members:</i></b>
//Loop to Display Member Edit Boxes
<?php
$i=1;
$memberarray = array('member1', 'member2', 'member3', 'member4', 'member5', 'member6', 'member7');
$rolearray = array('role1', 'role2', 'role3', 'role4', 'role5', 'role6', 'role7');
$mdescriptionarray = array('mdescription1', 'mdescription2', 'mdescription3', 'mdescription4', 'mdescription5', 'mdescription6', 'mdescription7');
$loginname = $_SESSION['valid_user'];
$_SESSION['no_of_influences'] = $HTTP_POST_VARS['member'];
$_SESSION['no_of_members'] = $HTTP_POST_VARS['influences'];
echo'<table border="0">';
while ($i <= $_SESSION['no_of_members'])
{
?>
<tr><td><p>
<strong>Member</strong></td>
<td><input type="text" name=<?" ' .$memberarray[i] .'"?> style="width: 175px">
</td></tr>
<tr><td>
<strong>Role</strong></td>
<td><input type="text" name=<?" ' .$rolearray[i] '"?> style="width: 175px">
</td></tr>
<tr><td>
<strong>Description</strong></td>
<td><input type="text" name=<?" '.$mdescriptionarray[i] '"?> style="width: 175px">
</td></tr>
$i++;
}
?>
<tr><td><b><i>Influences:</i></b></td></tr>
//Loop to Display Influence Edit Boxes
<?php
$n=1;
while ($n <= $_SESSION['no_of_influences'])
{
echo'<p><tr><td>
<strong>Influence</strong></td>
<td><input type="text" name="member ' . $n . '" style="width: 175px">
</td></tr>';
echo'<tr><td>
<strong>Description</strong></td>
<td><input type="text" name="description ' . $n . '" style="width: 175px">
</td></tr>';
$n++;
}
if(!empty($_POST)) {
$sql = "select band_ID from user where login_name = $loginname;
$result = mysql_query($sql);
$bandID = mysql_fetch_array($result);
//loop to insert Member Data into the Database
$i=1;
while ($i <= {$_SESSION['no_of_members']})
{
$member = {$_POST[ ' .$memberarray[i] .']};
$role= {$_POST[ ' .$rolearray[i] ']};
$description= {$_POST['.$mdescriptionarray[i] ']};
//THE ERROR IS HERE: Unexpected T_String on line 110, which is where the $bandID is.
$sql = "INSERT INTO `member` (`band_ID`, `member_name`)
VALUES (
'$bandID', '$member'
)";
mysql_query($sql, $dbh) or die("Failed <pre>$sql</pre>".mysql_error());
$memberID = mysql_insert_ID();
$sql = "INSERT INTO `memberrole` (`member_ID`, `role`)
VALUES(
'$memberID', '$role'
)";
mysql_query($sql, $dbh) or die("Failed <pre>$sql</pre>".mysql_error());
$sql = "INSERT INTO `role` (`role`, `description`)
VALUES(
'$role', '$description'
)";
mysql_query($sql, $dbh) or die("Failed <pre>$sql</pre>".mysql_error());
$i++
}
//Loop to Insert Influence Data into the Database
$n=1
while ($n <= {$_SESSION['no_of_influences']})
{
$influence = {$_POST['influence ' . $n . '']};
$description= {$_POST['description ' . $n . '']};
$sql = "INSERT INTO `bandinfluence` (`band_ID`, `influence`)
VALUES(
'$bandID', '$influence';
)";
mysql_query($sql, $dbh) or die("Failed <pre>$sql</pre>".mysql_error());
$sql = ""INSERT INTO `influence` (`influence`, `description`)
VALUES(
'$influence', '$description'
)";
mysql_query($sql, $dbh) or die("Failed <pre>$sql</pre>".mysql_error());
$n++
}
}
?>
<tr></tr>
<tr><td></td>
<td><input type="submit" name="submit" value="Submit Details" /></td></tr>
</table>
</td>
</tr>
<tr>
</form>
</tr>
</table>
</body>
</html>Another thing is that you may notice that I've used an array for the text field name on the member loops and just added $n to the end of the text field name on the influence loops. I wasn't sure if I could do the second thing so I was wondering firstly if the non array method will work and if it will work which method would work best and then I'll change them both to that method.
Any help would be greatly appreciated.
feyd | Please use [php] and
Code: Select all
tags where approriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]