Populate drop down list from mysql in in php html - php

I am using the below code to populate drop down list in php html,
<?php
$mid="mario";
$sql = "SELECT * FROM tbl_prdy WHERE col_master_id = '$mid'";
$result = mysqli_query($conn,$sql);
echo "<select name='list'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['col_of_fa'] . "'>" . $row['col_of_fa'] . "
</option>";
}
echo "</select>";
?>
But, I am getting internal server error. I have debugged the code and found that the issue is with the following 2 lines in the above code. There is not much information in server logs. Can you tell me what might be the issue with the following 2 lines of code?
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['col_of_fa'] . "'>" . $row['col_of_fa'] .
"/option>";
}

mixing mysqli with mysql
change
$row = mysql_fetch_array($result)
to
$row = mysqli_fetch_array($result)

You would use this
while($row=mysqli_fetch_assoc($result )){
}
or
while($row=mysqli_fetch_array($result )){
}

//Try this :
while ($row = mysqli_fetch_array($result)) { ?>
<option value="<?php echo $row['col_of_fa'] ?>" ><?php echo $row['col_of_fa'] ?>
</option>
<?php }

Related

how to get value from database and show in dropdown list

I already try many ways but the value didn't show in dropdown list
Here, this is my code. can you suggest me anything that i was wrong
<?php
$result = mysqli_query($con,"SELECT * FROM project");
if( mysqli_num_rows( $result )==0){
echo "<tr><td>No Rows Returned</td></tr>";
}else{
$row = mysqli_fetch_assoc( $result );
$pos = 0;
echo "<select name=Pname >";
while($pos <= count ($row)){
echo "<option value="$row["project_no"]">"$row["project_name"]"</option>";
$pos++;
}
echo "</select>";?>
And i write as .php file. Thanks for your help.
Try this out:
$output = '';
if(mysqli_num_rows($result) == 0){
// echo error;
} else {
while($row = mysqli_fetch_assoc($result)){
$project_no = $row['project_no'];
$project_name = $row['project_name'];
$output .= '<option value="' . $project_no . '">' . $project_name . '</option>";
}
}
Then inside of your HTML, print your $output variable inside of your <select> element:
<select>
<?php
print("$output");
?>
</select>
It should print all options for every row that you have requested from the database.
Hope this helps :)
Try this:
$result = mysqli_query($con,"SELECT * FROM project");
if( mysqli_num_rows( $result )==0){
echo "<tr><td>No Rows Returned</td></tr>";
}else{
echo "<select name=Pname >";
while ($row = mysqli_fetch_assoc($result)) {
echo "<option value="$row["project_no"]">"$row["project_name"]"</option>";
}
echo "</select>";
}
This is the result code that i can run it. I put this code in a form code of html
$result = mysqli_query($con,"SELECT * FROM project"); ?>
<?php
$output = '';
if(mysqli_num_rows($result) == 0){
// echo error;
} else {
echo " <select name = Pname>";
while($row = mysqli_fetch_assoc($result)){
$project_no = $row['project_no'];
$project_name = $row['project_name'];
$output = "<option value=" . $project_no . "> ". $project_name ." </option>";
print("$output");
}
echo " </select>";
}
?>
Thank you every one for helping me ^^

Retrieve value from DB and display in a drop down on edit form in php

This script does not display the DB value in a drop down on the edit form.
<?php
echo "<select name='assign' value=''><option>Select name</option>";
while ($r = mysql_fetch_array($result)) {
$value = $r['name'];
echo "<option value=" . $r['emp_id'] . ">" . $r['name'] . " if ($name=='$value') echo 'selected = 'selected''></option>";
}
echo "</select>";
It does not show any error. How it can write in a correct way.
You can try this :
$echoSting = '<select name="assign"><option value="">Select name</option>'.PHP_EOL;
while($r = mysql_fetch_array($result)) {
$value=$r['name'];
$echoSting .= '<option value="'.$r['emp_id'].'" '.($name==$value ? 'selected' : '').'>'.$r['name'].'</option>'.PHP_EOL;
}
$echoSting .= '</select>'.PHP_EOL;
echo $echoSting;
a side note, try looking into PDO for your database stuff : http://php.net/manual/en/book.pdo.php
Try this:
echo "<select name='assign' value=''><option>Select name</option>";
while($r = mysql_fetch_array($result)) {
$value=$r['name'];
echo "<option value='.$r['emp_id'].'>'.$r['name'].' "; if ($name=='$value') echo "selected = 'selected'";echo">$value</option>";
}
echo "</select>";

Find previously selected option in while loop

I'm trying to get a drop down menu to keep its selected value when the user hits submit, but it fails due to errors on the form.
I have a while loop returning values from a database to build the options for the drop down, but how do I echo "selected" on the right option?
I have tried if($district == $row["name"]) { echo "selected";} as you see below, but it doesn't work.
<?php
$result = mysql_query("SELECT dist.name FROM districts AS dist JOIN int_bd AS ibd ON dist.id = ibd.districts_id WHERE banners_id = 6 GROUP BY dist.id ORDER BY dist.id ASC", $connection);
if (!result) {
die("Database query failed: " . mysql_error());
}
while ($row = mysql_fetch_array($result)) {
echo '<option value="{$row["name"]}"'; if($district == $row["name"]) { echo "selected";} ; echo '>' . $row["name"] . "</option>";
}
?>
Sorry for the delay. None of the suggested answers worked for me. Any other ideas?
Can you try this,
<?php
$result = mysql_query("SELECT dist.name FROM districts AS dist JOIN int_bd AS ibd ON dist.id = ibd.districts_id WHERE banners_id = 6 GROUP BY dist.id ORDER BY dist.id ASC", $connection);
if (!result) {
die("Database query failed: " . mysql_error());
}
$district = $_REQUEST['name']; // You need pass the value you have been submitted
while ($row = mysql_fetch_array($result)) {
$selected ="";
if(trim($district) == trim($row["name"])) { $selected = "selected";}
echo '<option value="{$row["name"]}" '.$selected.' >' . $row["name"] . "</option>";
}
?>
Try this..
if($district == $row["name"])
{
echo "<option value='$district' selected>$district</option>";
}
I just found the answer. This is what I did:
while ($row = mysql_fetch_array($result)) {
echo '<option value="' . $row["name"] . '"';
if($row["name"] == $district) { echo 'selected';} ;
echo '>' . $row["name"] . '</option>';
}
It seems to have been this line
echo '<option value="{$row["name"]}"';
that was causing the problem.

Trouble storing value of drop down, and using in query

Hi I am having trouble getting this to store the value when submitted and then using it in my next query. The drop down is populated fine, but it is either not storing the value or the way I am using it dynamically in my query is wrong. Any help is appreciated. I know I am probably missing something obvious or small, I am new to php.
drop down box
$sth = $db->prepare("SELECT DISTINCT WEEK FROM Player_Points_2013");
//$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
$sth->execute();
echo '<form method="POST" action=" " >';
echo '<select id="week" name="week"><OPTION>';
echo "Select a Week to see the stats</OPTION>";
while ($row = $sth->fetch(PDO::FETCH_ASSOC))
{
echo "<option value=\"$WEEK\">" . $row['WEEK'] . "</option>";
}
echo '</SELECT>';
echo '<input type="submit" name="table_stats" value="Submit"/>';
echo '</form>';
$select_val = $_POST['week'];
//use value from drop down to display weeks stats
if(isset($_POST['table_stats'])){
$sql = $db->prepare("SELECT PName,CombinedPoints FROM `Player_Points_2013` WHERE
WEEK = '$select_val' ORDER BY CombinedPoints ");
//$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
$sql->execute();
Print "<table border cellpadding=10 width=500>";
Print "<tr><th>Player</th><th>Points</th></tr>";
while ($row = $sql->fetch(PDO::FETCH_ASSOC))
{
Print " <td>".$row['PName'] . "</td> ";
Print " <td>".$row['CombinedPoints'] . "</td> </tr>";
}
Print "</table>";
}
else {
echo $select_val;
}
?>
Use either $row or $info but not both, e.g.:
while ($row = $sql->fetch(PDO::FETCH_ASSOC))
{
Print " <td>".$row['PName'] . "</td> ";
Print " <td>".$row['CombinedPoints'] . "</td> </tr>";
}
Also you have code:
echo "<option value=\"$WEEK\">" . $row['WEEK'] . "</option>";
What is $WEEK? Maybe it should be $row['WEEK'] instead?

dynamic dropdown filled from mysql data

What i'm trying to do is display a drop down with all field names from mysql database, once the user picks one and submits the form i want to display a second dropdown filled with all the rows from the submitted field name, this is my code so far:
$result = mysql_query("select * from `parts`") or die(mysql_error());
echo "<form action='".$_SERVER['PHP_SELF']."' method='post'>";
echo "<select name='field_names'>";
$i = 0;
while ($i < mysql_num_fields($result)) {
$fieldname = mysql_field_name($result, $i);
echo '<option value="'.$fieldname.'">'.$fieldname.'</option>';
$i++;
}
echo "</select>";
echo "<input type='submit' value='submit'></input>";
echo "</form>";
if($_POST) {
$fields = $_POST['field_names'];
$result1 = mysql_query("select '".$fields."' from `parts`") or die(mysql_error());
echo '<select name="fields">';
while ($row = mysql_fetch_array($result1)) {
echo "<option value=".$row[$fields].">".$row[$fields]."</option>";
}
echo '</select>';
}
Can anyone spot where i'm going wrong, thanks
there is a mistake on the line number 29
$result1 = mysql_query("select '" . $fields . "' from `parts`") or die(mysql_error());
you are using ' instead of `. Do as follows
$result1 = mysql_query("select `" . $fields . "` from `parts`") or die(mysql_error());
Hope your problem is solved.
As it stands now, the second set of selects will be issued OUTSIDE of your </form> tag, so will never get submitted with the rest of the form. At best, you should move the form closing tag to below the POST handler.
here database details
mysql_connect('hostname', 'username', 'password');
mysql_select_db('database-name');
$sql = "SELECT username FROM userregistraton";
$result = mysql_query($sql);
echo "<select name='username'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['username'] ."'>" . $row['username'] ."</option>";}
echo "</select>";
here username is the column of my table(userregistration)
it works perfectly

Categories