Skip to content Skip to sidebar Skip to footer

Php- Fetch From Database And Store In Drop Down Menu Html

I can't seem to get the following code to make a dropdown menu that contains data from a mysql database. The 'include('connect.php');' connects to the mysql database and I know it

Solution 1:

<?phpinclude('connect.php');

$query = "SELECT Cnum, CName FROM Customer";
$result = mysql_query ($query);
echo"<select name='dropdown' value=''><option>Dropdown</option>";
while($r = mysql_fetch_array($result)) {
  echo"<option value=".$r['Cnum'].">".$r['CName']."</option>"; 
}
echo"</select>";
?>

From the looks of things, you're missing an opening option tag, so it's just outputting "Dropdown" as a line of text.

Edit

Just to be completely transparent, because I did not have connect.php, I had to add my own DB connections. My whole page looked thusly:

<?//Adding to display errors.
error_reporting(E_ALL);
ini_set('display_errors', '1');
?><HTML><HEAD></HEAD><BODY><H1>Find Customer's Albums Page</H1>
 From a dropdown list of customers, a user should be able to pick a customer and see a list of albums (all fields in the CD table) purchased by that customer.
 <HR><FORMACTION="listCustomer.php"METHOD="POST"/>
 Customer:
 <selectname="mydropdownCust"><optionvalue="101">101</option><optionvalue="102">102</option><optionvalue="103">103</option><optionvalue="104">104</option><optionvalue="105">105</option><optionvalue="106">106</option><optionvalue="107">107</option><optionvalue="108">108</option><optionvalue="109">109</option><optionvalue="110">110</option></select><BR /><?php// BEGIN ADDED CONNECTION HACKY GARBAGE$con=mysql_connect("localhost","root","root");
  // Check connectionif (mysqli_connect_errno($con)) {
    echo"Failed to connect to MySQL: " . mysqli_connect_error();
  }
  $selected = mysql_select_db("sample",$con) 
    ordie("Could not select examples");
  // END ADDED CONNECTION HACKY GARBAGE$query = "SELECT Cnum, CName FROM Customer";
  $result = mysql_query ($query);
  echo"<select name='dropdown' value=''><option>Dropdown</option>";
  while($r = mysql_fetch_array($result)) {
    echo"<option value=".$r['Cnum'].">".$r['CName']."</option>"; 
  }
  echo"</select>";
 ?><BR /><INPUTTYPE="SUBMIT"Value="Submit"/></FORM><FORMACTION="listMenu.html"METHOD="POST"/><INPUTTYPE="SUBMIT"Value="Main Menu"/></FORM></BODY></HTML>

Solution 2:

First off, you are missing an option opening tag, as correctly mentioned by stslavik. But this is not causing the issue here as it seems (it's auto-corrected by the browser - in my tests atleast).

Secondly, this wont work (problem causer):

echo"<option value=$r["Cnum"]>$r["CName"]</option>"; 

You should use

echo"<option value=".$r["Cnum"].">".$r["CName"]."</option>"; 

or, as I always prefer single quotes to enclose echo or print output strings:

echo'<option value='.$r['Cnum'].'>'.$r['CName'].'</option>';

Third alternative (complex syntax: What does ${ } mean in PHP syntax?)

echo"<option value={$r["Cnum"]}>{$r["CName"]}</option>";

Solution 3:

assuming you get data from the database try this

echo"<option value={$r['Cnum']}>{$r['CName']}</option>"; 

Solution 4:

try,

echo"<option value=' . $r['Cnum'] . '>' . $r['CName'] . '</option>"; 

instead of

echo"<option value=$r[Cnum]>$r[CName]</option>";

Post a Comment for "Php- Fetch From Database And Store In Drop Down Menu Html"