Listbox problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jaxxstorm
Forum Newbie
Posts: 1
Joined: Fri Aug 29, 2008 7:56 am

Listbox problem

Post by jaxxstorm »

I'm new to PHP, and creating a form with a listbox that is populated from a mySQL table.
I know this is fairly common, but I'm having a problem getting the listbox to populate;
Here is the code I'm using
Database connection php

Code: Select all

<?php
$dbServer="localhost";
$username="username";
$password="password";
$database="database";
//make database connection
$conn = mysql_connect($dbServer,$username,$password);
@mysql_select_db("$database") or die("unable to select database".mysql_error());
 
?>
Listbox code

Code: Select all

 
<label>Resource Required
<select name="Resource">
<option value=>--Select--</option>
<?
//get records from database(table resource)
$list=mysql_query("SELECT * FROM Resource");
 
//show records by while loop
while($row_list=mysql_fetch_array($list))
?>
<optionvalue="<? echo $row_list[Res_ID]; ?>" </option>
</select>
 
 
The select option is appearing with no problems, but the listbox isn't populating with the information from the database, can anybody see the problem?
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: Listbox problem

Post by susrisha »

i would recommend you put an if(!mysql_query) statement and then echo mysql_error();
it might show up some error which is not getting displayed at present.. and if u do find an error.. do let us know
DaiWelsh
Forum Commoner
Posts: 36
Joined: Wed Jan 08, 2003 9:39 am
Location: Derbyshire, UK

Re: Listbox problem

Post by DaiWelsh »

Two problems jump out, though there may be more:

<option value=>--Select--</option>

you have no value, it should be

<option value="">--Select--</option>

and then in the loop

<optionvalue="<? echo $row_list['Res_ID']; ?>" </option>

you have no space after the tag name and have not closed closed the tag, it should be

<option value="<? echo $row_list['Res_ID']; ?>"> </option>

Probably you also want a label for the option but I don't know what field that should be so for illustration

<option value="<? echo $row_list['Res_ID']; ?>">="<? echo $row_list['Res_ID']; ?></option>

will put the id in both the value and label/text of the option.

Note I have added apostrophes around the field name in the array index as otherwise you are relying on PHP to assume this when it can't find a constant called Res_ID which will generate a warning.

HTH,

Dai
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Listbox problem

Post by RobertGonzalez »

Yeha, you are not building your option elements properly. For the record, they should look like:

Code: Select all

<select name="selectthis">  <option vlaue="val1">Option1</option>  <option vlaue="val2">Option2</option>  <option vlaue="val3">Option3</option></select>
Your current output is not at all like this. I would also suggest running your output through the HTML validator at w3.org. It would have spotted that right away.
Post Reply