Can someone show me where I go wrong. I am trying to populate a drop down list with field "type_desc" from the [U]eventType[/U] table. Upon selection, the value that will be passed will be "type_code". All I am getting is a blank drop list. Can someone help.

Tablename: eventType
Fields:
type_code type=int auto-increase
type_desc type=text

<?
include 'db.php';
$sql_event_type = "select type_code,type_desc from eventType";
$sql_event_type_result = mysql_query($sql_event_type,$connection) 
    or die("Couldn't execute query.");

if ($sql_event_type_result) {
    echo "<SELECT NAME='type_code'>";
    while ($type_row = mysql_fetch_array($sql_event_type_result)){
    echo "<OPTION VALUE=\"".$type_row["type_code"]."\">".
     $type_row["type_desc"]." </OPTION> ";
    }
    echo "</SELECT>";
}
?>

Thanks
tip

while ($type_row = mysql_fetch_array($sql_event_type_result)){

Should this not be as below? (mysql_fetch_array to mysql_fetch_row)

while ($type_row = mysql_fetch_row($sql_event_type_result)){

OK Roberdin. No it still does not make any difference.

tip

Are you syre that the query is returning a result?

Yes it returns a result. Correct me if I am wrong, but if it doesn't it won't pass the if statement test and won't display a blank list. Been working on this all morning without much success.

ta

tip

Actually it will. It's a valid result handle, irrelevant of its contents.

<?
include 'db.php';
$sql_event_type = "
                SELECT
                    type_code,
                    type_desc
                FROM
                    eventType";
$sql_event_type_result = mysql_query($sql_event_type,$connection) or die("Couldn't execute query.");

// Debug
echo 'We have '.mysql_num_rows($sql_event_type_result).' in the database<br />';

if ($sql_event_type_result)
{
    echo "<SELECT NAME='type_code'>";
    while ($type_row = mysql_fetch_array($sql_event_type_result))
    {
        echo "<option value='".$type_row['type_code']."'>".$type_row['type_desc']."</option>";   
    }
    echo "</SELECT>";
}
?>

Cleaned up the formatting, removed the horrible escaping characters (you don't need these those if you differentiate between ' and " .. and added a debug call to make sure you are actually getting a result set back :)

Thank you very much Shorty. I have another question - this code works if a new event is added, where its type is selected from the list. How can I modify the code to enable me to edit an existing event with its type.

Thanks again Shorty

tip

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.