parse error with drop down menu

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
marker5a
Forum Commoner
Posts: 29
Joined: Wed May 19, 2004 9:18 pm

parse error with drop down menu

Post by marker5a »

Ok
I have this database with some contact names of people. I am trying to make a form that will take those names from the database and put it into a drop down menu. I have the script here:

<?php
$db = mysql_connect("localhost", "root", "***");
mysql_select_db("xc_contact",$db);
$sql="SELECT FirstName,LastName,contact_id FROM `names`";
$result=mysql_query($sql,$db);
while($row=mysql_fetch_array($result))
{
$FirstName = $row["FirstName"];
?>

<form method="POST" action="www.marker5a.com/database/logger2.php">

<p><select size="1" name="menu">
<option><?php echo $FirstName; ?></option>
&nbsp;
</select><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p>
</form>


I am new at Php, so I am not sure if this is correct. I keep getting a parse error. The link is http://www.marker5a.com/database/logger.php

I appreciate all of the help I can get. Thank you

Chris
choppsta
Forum Contributor
Posts: 114
Joined: Thu Jul 03, 2003 11:11 am

Post by choppsta »

I think it may have something to do with a missing brace on your while loop. But the main problem is that you're only going to get one option in your select box, the last one in the list.

The following builds the option list first, then drops it into the HTML...

Code: Select all

<?php

$db = mysql_connect("localhost", "root", "***"); 
mysql_select_db("xc_contact",$db); 
$sql = "SELECT FirstName, LastName, contact_id FROM `names`"; 
$result = mysql_query($sql, $db); 
while ($row = mysql_fetch_array($result)) { 
	$options .= '<option>'.$row["FirstName"].'</option>'; 
}

echo '<form method="POST" action="www.marker5a.com/database/logger2.php"><p><select size="1" name="menu">';
echo $options;
echo '</select><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p></form>';

?>
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

first of all im gonna clean up your code formatting

Code: Select all

<?php 
$db = mysql_connect("localhost", "root", "***"); 
mysql_select_db("xc_contact",$db); 
$sql="SELECT FirstName,LastName,contact_id FROM `names`"; 
$result=mysql_query($sql,$db); 
while($row=mysql_fetch_array($result)) 
{ 
$FirstName = $row["FirstName"]; 
?> 

<form method="POST" action="www.marker5a.com/database/logger2.php"> 

<p><select size="1" name="menu"> 
<option><?php echo $FirstName; ?></option> 
  
</select><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p> 
</form>
try this

Code: Select all

<?php 
$db = mysql_connect("localhost", "root", "***"); 
mysql_select_db("xc_contact",$db); 
$sql="SELECT FirstName,LastName,contact_id FROM `names`"; 
$result=mysql_query($sql,$db); 
while($row=mysql_fetch_array($result)) 
{ 
$FirstName = $row["FirstName"]; 
print "
<form method="POST" action="www.marker5a.com/database/logger2.php"> 

<p><select size="1" name="menu"> 
<option>$FirstName</option> 
</select><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p> 
</form>":
else
{
// error handler? 
}
for your information reason you were getting parse errors is because you didnt have a }
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

err i think your logic is a little off as well:

Code: Select all

<?php

$db = mysql_connect("localhost", "root", "***"); 
mysql_select_db("xc_contact",$db); 
$sql="SELECT FirstName,LastName,contact_id FROM `names`"; 
$result=mysql_query($sql,$db); 

print " 
<form method="POST" action="www.marker5a.com/database/logger2.php"> \n

<p><select size="1" name="menu">\n ";

while($row=mysql_fetch_array($result)) 
{ 
$FirstName = $row["FirstName"]; 
print " 
  <option>$FirstName</option>\n ";
}
print"
</select><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p> 
</form>": 
else 
{ 
// error handler? 
} 




?>
marker5a
Forum Commoner
Posts: 29
Joined: Wed May 19, 2004 9:18 pm

Post by marker5a »

Hey
I used the script, and was getting another error, so I cleared out the last portion minus the ?> part, and it works like a charm now. Thanks a lot, I really appreciate the help.

Chris
Post Reply