0

I just started learning php last week and I am trying to populate values of a drop down list in my html form from mysql database. the dropdown shows but it only shows "choose" as an option. what am I doing wrong, any help would be march appreciated. Below is the problem part of my code.

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

 <?php
  require_once('connectvars.php');
  $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
  $query = "SELECT event_id, event_name FROM events";

  $results= mysqli_query( $dbc, $query);

  $options="";

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

 $id=$row["event_id"];
 $event=$row["event_name"];
 $options ="<OPTION VALUE=\"$id\">".$event;
 } 
?>


 <SELECT NAME=eventid>
<OPTION VALUE=0>Choose
  <?php echo $options ?>
 </SELECT> 
</form>

5 Answers 5

1

This is the mysql-driver solution.

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php
    require_once('connectvars.php');
    $dbc = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
    $db = mysql_select_db(DB_NAME);
    $results= mysql_query("SELECT event_id, event_name FROM events");
?>
<select name="eventid">
    <option value="0">Choose</OPTION>
    <?php 
        while($row = mysql_fetch_array($results)) {
            echo '<option value="'.$row['event_id'].'">'. $row['event_name'].'</option>';
        }
    ?>
</select> 
</form>
Sign up to request clarification or add additional context in comments.

Comments

1

change

$options ="<OPTION VALUE=\"$id\">".$event;

to

$options .="<OPTION VALUE=\"$id\">".$event;

to concatenate instead of changing the value always.. also consider closing the option tag..

2 Comments

alright changed that part to $options .="<OPTION VALUE=\"$id\">".$event. "</options>"; but still the same result
and you sure that results are being fetched? for example: I see no error handling.. try something like: if (!$results) echo mysql_error($dbc); and see if you get any message
1

Include correction after Benedikt Olek's answer.

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

<?php
 require_once('connectvars.php');
 $dbc = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
 $query = "SELECT event_id, event_name FROM events";
 $db = mysql_select_db(DB_NAME);
 $results= mysql_query( $dbc, $query);
?>


<SELECT NAME=eventid>
<OPTION VALUE=0>Choose</OPTION>
  <?php 
     while($row = mysql_fetch_array($results)
        echo "<option value=\"" . $row['event_id'] . "\">" . $row['event_name'] . "</option>";
  ?>
</SELECT> 
</form>

2 Comments

@paabobo As in 500 Internal Server Error?
@paabobo In the while loop condition, it should be $results instead of $result. I have corrected in the code
0

try this one :

 <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
 <SELECT NAME=eventid>
 <?php
  require_once('connectvars.php');
  $dbc = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
  mysql_select_db(DB_NAME);
  $query = "SELECT event_id, event_name FROM events";

  $results= mysql_query( $dbc, $query);

  $options="";

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

 $id=$row["event_id"];
 $event=$row["event_name"];
echo "<OPTION VALUE=".$id\.">".$event."</option>";
 } 
?>

 </SELECT> 
</form>

Comments

0

Since "Choose" is a hardcoded option it is always displayed. You are not getting more results because the Variable $options is empty. You can verify that by using var_dump($options).

The reason for your empty variable is the while-iteration. Your function mysql_fetch_array() never returns true.

That is because you are mixing up drivers. You are connecting via mysqli, hence mysql_fetch_array is not working.

You have two options: change to use the mysql-driver for connection or recode the iteration to make use of the mysqli-api.

Comments

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.