Page 1 of 1

parse error with drop down menu

Posted: Thu Jun 03, 2004 8:54 am
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

Posted: Thu Jun 03, 2004 9:16 am
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>';

?>

Posted: Thu Jun 03, 2004 9:17 am
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 }

Posted: Thu Jun 03, 2004 9:39 am
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? 
} 




?>

Posted: Thu Jun 03, 2004 8:40 pm
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