Jump to content

Displaying data from a mysql dropdown selection


rickxphp

Recommended Posts

Hi all, I'm trying to display a form based on a dropd own selection. The drop down is propagated from a database query. I have the drop down list displaying, but when I click submit, I can't get the result to display.

 

$sql="SELECT id, company_name FROM webprojects ORDER BY company_name ASC";
$result=mysql_query($sql);

$options=""; 

while ($row=mysql_fetch_array($result)) {
    $id=$row["id"];
    $company_name=$row["company_name"];
    $options.="<OPTION VALUE=\"$id\">".$company_name;
} 

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHP Dynamic Drop Down Menu</title>
</head>

<body>

<form id="form1" name="form1" method="post" action="selected.php">

<SELECT NAME=id>
<OPTION VALUE=0>Choose
<?=$options.="<OPTION VALUE=\"$id\">".$company_name.'</option>';?>
</SELECT>

  <label>
  <input type="submit" name="submit" id="submit" value="Submit" />
  </label>
</form>

</body>
</html>

Any help is greatly appreciated

Link to comment
Share on other sites

Hi thanks I've done some more work on my scripts.

I'm not even sure if this should be posted here now!

but here it goes.

I have a working dropdown, being populated from mysql.

The issue I have is when I try and query to display my choice.

query.php

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');

$dbname = 'webform';
mysql_select_db($dbname);


@mysql_select_db($dbname) or die( "Unable to select database");

$query="SELECT id,company_name FROM webprojects";

$result = mysql_query ($query);

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

while($nt=mysql_fetch_array($result)){
echo "<option value=$nt[id]>$nt[company_name]</option>";
}
echo "</select>";// Closing of list box
echo "
<input type=submit value=submit name=button>
</form>";
?> 
[code]
data.php
[code]
mysql_select_db($dbname);


@mysql_select_db($dbname) or die( "Unable to select database");

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

$query = "SELECT 'company_name' FROM webprojects WHERE 'company_name'='" . $_POST['company_name'] ."'";

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

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

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

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

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

?> 
[code]

I'm not sure where to go from here Thanks.

Link to comment
Share on other sites

Right on Thanks Everybody!!!!!!!!!!!

It is passing the id and not the table values. ex company name for for the next query.

dropdown is populated from mysql

You select a name and click submit

script does a query finds that company name in company_name table and displays it.

The script will grow from here, but I need to start somewhere.

query.php

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');

$dbname = 'webform';

@mysql_select_db($dbname) or die( "Unable to select database");

$query="SELECT id,company_name FROM webprojects";

$result = mysql_query ($query);

echo "<form name=form method=post action='data.php'>";
echo "<select name=\"company_name\">";

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

?> 

data.php

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');

$dbname = 'webform';

mysql_select_db($dbname) or die( "Unable to select database");

$company_name=$_POST['company_name'];

if(isset($_POST['button']))
{
echo "<tr> <th>Company Name ".$company_name. "</th></tr>";
$query = "SELECT 'company_name' FROM webprojects WHERE 'company_name'='" . $company_name ."'";

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

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

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

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

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

?> 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.