Reputation: 913
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>
Upvotes: 0
Views: 4891
Reputation:
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>
Upvotes: 1
Reputation: 2009
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>
Upvotes: 1
Reputation:
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.
Upvotes: 0
Reputation: 1
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>
Upvotes: 0
Reputation: 5397
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..
Upvotes: 1