Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Drop down menu and MySQL

Status
Not open for further replies.

dkin

Programmer
Dec 11, 2002
39
ES
Hello,

I tried to build a drop down menu that its content is a MySQL data. Unfortunately it does not work.

Can anyone tell me what the problem is?


Script (HTML file):

<html>
<head>
<title> Untitled </title>

</head>
<body>
<tr>

<td height=&quot;86&quot;></td>
<td colspan=&quot;4&quot; rowspan=&quot;2&quot; valign=&quot;top&quot;><select name=&quot;spec[]&quot; size=&quot;6&quot; multiple id=&quot;spec&quot;>
<option value=&quot;All&quot; selected>All
<?php

$host = 'localhost';
$user = 'root';
$pass = '';
$db = 'car';
$table = 'list';

$link = mysql_connect($host,$user,$pass);
if(!$link) die(mysql_error());
$select = mysql_select_db($db);
if(!$select) die(mysql_error());

$query = &quot;SELECT species FROM list&quot;;
$result = mysql_query($query);
if(!$result) die(mysql_error());
echo &quot;<select name=\&quot;species\&quot;>\n&quot;;
echo &quot;<option value=\&quot;\&quot;> </option>\n&quot;;
while($row = mysql_fetch_array($result))
{
echo &quot;<option value=\&quot;$row[species]\&quot;>$row[species]</option>\n&quot;;
}
echo &quot;</select>\n&quot;;
echo &quot;<P><input type=\&quot;submit\&quot; value=\&quot;submit\&quot;></p>\n&quot;;
?>

</select>
</select>

</tr>
</body>
</html>
 
change this line

echo &quot;<option value=\&quot;$row[species]\&quot;>$row[species]</option>\n&quot;;

to this:

echo &quot;<option value=\&quot;$row[species]\&quot;>$row[\&quot;species\&quot;]</option>\n&quot;;

notice the quotes around the field name you want to pull from your database

hope this helps!
 
i much prefer to do the following:

echo &quot;<option value='&quot;.$row['species'].&quot;'>&quot;.$row['species'].&quot;</option>\n&quot;;

Quotes around array elements, array items to be used outside of the string. --BB
 
Thanks, Tested the correction, but the results form the while loop do not send them to the drop down menu but outside of it.

How can I fix it.

Thanks

Script:
<html>
<head>
<title> Untitled </title>

</head>
<body>
<tr>

<td height=&quot;86&quot;></td>
<td colspan=&quot;4&quot; rowspan=&quot;2&quot; valign=&quot;top&quot;><select name=&quot;spec[]&quot; size=&quot;6&quot; multiple id=&quot;spec&quot;>
<option value=&quot;All&quot; selected>All
<?php

$host = 'localhost';
$user = 'root';
$pass = '';
$db = 'g2mod';
$table = 'main';

$link = mysql_connect($host,$user,$pass);
if(!$link) die(mysql_error());
$select = mysql_select_db($db);
if(!$select) die(mysql_error());

$query = &quot;SELECT species FROM main&quot;;
$result = mysql_query($query);
if(!$result) die(mysql_error());
echo &quot;<select name=\&quot;species\&quot;>\n&quot;;
echo &quot;<option value=\&quot;\&quot;> </option>\n&quot;;
while($row = mysql_fetch_array($result))
{
echo &quot;<option value='&quot;.$row['species'].&quot;'>&quot;.$row['species'].&quot;</option>\n&quot;;
}
echo &quot;</select>\n&quot;;
echo &quot;<P><input type=\&quot;submit\&quot; value=\&quot;submit\&quot;></p>\n&quot;;
?>

</select>
</select>

</tr>
</body>
</html>
 
Tested the correction, but the results form the while loop do not send them to the drop down menu but outside of it.

How can I fix it.

Thanks

Script:
<html>
<head>
<title> Untitled </title>

</head>
<body>
<tr>

<td height=&quot;86&quot;></td>
<td colspan=&quot;4&quot; rowspan=&quot;2&quot; valign=&quot;top&quot;><select name=&quot;spec[]&quot; size=&quot;6&quot; multiple id=&quot;spec&quot;>
<option value=&quot;All&quot; selected>All
<?php

$host = 'localhost';
$user = 'root';
$pass = '';
$db = 'g2mod';
$table = 'main';

$link = mysql_connect($host,$user,$pass);
if(!$link) die(mysql_error());
$select = mysql_select_db($db);
if(!$select) die(mysql_error());

$query = &quot;SELECT species FROM main&quot;;
$result = mysql_query($query);
if(!$result) die(mysql_error());
echo &quot;<select name=\&quot;species\&quot;>\n&quot;;
echo &quot;<option value=\&quot;\&quot;> </option>\n&quot;;
while($row = mysql_fetch_array($result))
{
echo &quot;<option value='&quot;.$row['species'].&quot;'>&quot;.$row['species'].&quot;</option>\n&quot;;
}
echo &quot;</select>\n&quot;;
echo &quot;<P><input type=\&quot;submit\&quot; value=\&quot;submit\&quot;></p>\n&quot;;
?>

</select>
</select>

</tr>
</body>
</html>
 
You have a lot of basic HTML mistakes in your code. I suggest you pick up a HTML book and read it... //Daniel
 
Here is the PHP code that I have used previously to make a dropdown list from a MySQL database. This was used to add data to existing tables in a multi-table database. It's only partial, but I think you can get the rest of it figured out.

$conn = @mysql_connect(&quot;HOST&quot;,&quot;USER&quot;,&quot;PASSWORD&quot;) or die(&quot;Could not connect.&quot;);
$db = @mysql_select_db(&quot;DATABASE&quot;,$conn) or die(&quot;Could not open database.&quot;);
$table = @mysql_list_tables(&quot;DATABASE&quot;) or die(&quot;Could not list tables.&quot;);

$step_one = &quot;
<FORM METHOD=\&quot;post\&quot; ACTION=\&quot;$PHP_SELF\&quot;>
<P>Add item to:
<SELECT NAME=\&quot;tname\&quot;>&quot;;

for($tc = 0; $tc < mysql_num_rows($table); $tc++) {
$table_name[$tc] = mysql_tablename($table, $tc);
$step_one .= &quot;<OPTION VALUE=\&quot;$table_name[$tc]\&quot;>$table_name[$tc]</OPTION>&quot;;
}

$step_one .= &quot;
</SELECT>
<INPUT TYPE=\&quot;submit\&quot; NAME=\&quot;submit\&quot; VALUE=\&quot;Add Item\&quot;></P>
</FORM>&quot;;

echo $step_one;
 
Get rid of the two extraneous closing <select> tags...

</select>
</select>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top