3

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 works on separate pages. Any suggestions?

Below is the entire code. listCustomer

 <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>
 <FORM ACTION="listCustomer.php" METHOD="POST"/>
 Customer:
 <select name="mydropdownCust">
 <option value="101">101</option>
 <option value="102">102</option>
 <option value="103">103</option>
 <option value="104">104</option>
 <option value="105">105</option>
 <option value="106">106</option>
 <option value="107">107</option>
 <option value="108">108</option>
 <option value="109">109</option>
 <option value="110">110</option>
 </select>
 <BR>

 <?php
 include('connect.php');

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

 <BR>
 <INPUT TYPE="SUBMIT" Value="Submit"/>
 </FORM>

 <FORM ACTION="listMenu.html" METHOD="POST"/>
 <INPUT TYPE="SUBMIT" Value="Main Menu"/>
 </FORM>
 </BODY>
 </HTML>
9
  • could you share what are you getting as a result? Furthermore, I think this Dropdown</option> after the end of the <select> tag is wrong. Maybe you mean <option>Dropdown</option> Commented May 22, 2013 at 21:17
  • so whats not working? Commented May 22, 2013 at 21:17
  • does it work better with this? {$r['Cnum']} Commented May 22, 2013 at 21:17
  • It displays this "Dropdown"; while($r = mysql_fetch_array($result)){ echo ""; } echo ""; ?>" on the website. Commented May 22, 2013 at 21:23
  • I don't think the variable syntax is your issue (just tested the echo locally with the way you've constructed your echo, try a var_dump on $r inside your loop Commented May 22, 2013 at 21:28

4 Answers 4

5
<?php
include('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>
 <FORM ACTION="listCustomer.php" METHOD="POST"/>
 Customer:
 <select name="mydropdownCust">
 <option value="101">101</option>
 <option value="102">102</option>
 <option value="103">103</option>
 <option value="104">104</option>
 <option value="105">105</option>
 <option value="106">106</option>
 <option value="107">107</option>
 <option value="108">108</option>
 <option value="109">109</option>
 <option value="110">110</option>
 </select>
 <BR />
 <?php
  // BEGIN ADDED CONNECTION HACKY GARBAGE
  $con=mysql_connect("localhost","root","root");
  // Check connection
  if (mysqli_connect_errno($con)) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
  $selected = mysql_select_db("sample",$con) 
    or die("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 />
 <INPUT TYPE="SUBMIT" Value="Submit"/>
 </FORM>

<FORM ACTION="listMenu.html" METHOD="POST"/>
<INPUT TYPE="SUBMIT" Value="Main Menu"/>
</FORM>
</BODY>
</HTML>
Sign up to request clarification or add additional context in comments.

4 Comments

Also missing quotes around dropbox in the name attribute. For consistency at least.
I can't make it work. Here is what it looks like. imageshack.us/photo/my-images/441/10517841.jpg
... Really? I created a 2 row table, with 2 columns Cnum (an int) and CName (a varchar 20 chars long), and the above (with a couple changes since I don't have your connect.php) pulled the info into a drop down. You uploaded this as printed? And you cleared your cache?
could it be that I copied a lot of the code instead of typed it out?
4

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>";

Comments

1

assuming you get data from the database try this

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

6 Comments

What are you trying to do here? First line is identical with the same line in the question - it ends the string prematurely.
sorry the code got messed up what is what i was trying to say is that you need to use the {} for complex expressions
Why would you like those parenthesis here!? That will just wrap the variable in {} strings.
@FrederikWordenskjold do you know php don't you see he is using double quotes so instead of using single quotes echo "<option value=$r["Cnum"]"; this will be error as the compiler will get confused the {} braces are just for clarity
what do you mean it doesnt work what part doesnt work the code i gave you is perfectly ok you didn't even ask the question you ass @FrederikWordenskjold
|
-1

try,

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

instead of

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

1 Comment

Parse error: syntax error, unexpected '"', expecting T_STRING or T_VARIABLE or T_NUM_STRING in...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.