Forum Moderators: coopster

Message Too Old, No Replies

dropdown menu from mysql table

         

sam10178

7:59 pm on Jan 6, 2009 (gmt 0)

10+ Year Member



I edited this post to make it more simple and clearer. I have a database named fruits with the table named list that looks like this.

nameid ¦ name ¦ date
1 Apples Jan
2 Oranges Feb
3 Pears March

What I would like to do is pull the name from the table and make it a drop down menu and have a submit button. So if the user chooses Apples and submits, it would return with the date Jan. Oranges Feb. etc. I have the drop down menu below that pulls it from the database, but I can't figure out how to get the submit button working that would send it to another php file (data.php) that will output Jan.

form.php
<?php
$localhost="localhost";
$username="#*$!";
$password="yyy";
$database="fruits";

$linkid=mysql_connect($localhost,$username,$password);
mysql_select_db($database) or die( "Unable to select database");

$query="SELECT name,nameid FROM list";

$result = mysql_query ($query);

echo "<form name=form method=post action='data.php'>
<select name=fruit>Fruit Name";

while($nt=mysql_fetch_array($result)){
echo "<option value=$nt[fruitid]>$nt[fruit]</option>";
}
echo "</select>";// Closing of list box
echo "
<input type=submit value=Submit name=button>
</form>";

?>

?>

data.php
<?php
$localhost="localhost";
$username="#*$!";
$password="yyy";
$database="fruits";

$linkid=mysql_connect($localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

if(isset($_POST['button']))
{

$query = "SELECT * FROM list WHERE name='" . $_POST['name'] ."'";

$result = mysql_query($query) or die(mysql_error());

echo "<table border='1'>";
echo "<tr> <th>Name</th> <th>Date</th> </tr>";

while($row = mysql_fetch_array($result))
{

echo "<tr><td>";
echo $row['namer'];
echo "</td><td>";
echo $row['date'];
echo "</td></tr>";
}

echo "</table>";
}
else{
}

?>

Can someone tell me how to do this please. Right now when I hit submit, all it does is give me an empty table. I think the error is around here:

if(isset($_POST['button']))
{

$query = "SELECT * FROM list WHERE name='" . $_POST['name'] ."'";

in data.php, but i'm not 100% sure.

rob7591

11:54 pm on Jan 6, 2009 (gmt 0)

10+ Year Member



echo "<option value=$nt[fruitid]>$nt[fruit]</option>

Shouldn't that be $nt[nameid] and $nt[name]?

Also, why don't you just set the value = to $nt[date] and then post that value on data.php, it will save you a MySQL query.

For example:


Fruit Name: <select name="fruit">
<?
while($row = mysql_fetch_assoc($result)) {
?>
<option value="<?=$row['date']?>">$row['fruit']</option>
<? } ?>

data.php


<?=$_POST['fruit']?>