How to populate select options from DB - php

What am I doing wrong here? When it populates the options it shows $name[0] and not the info from the DB. Though the correct number of options seem to be available.
<?php
//connect to database
$conn = mysqli_connect("example.com", "timemin", "Pass123", "timesheet");
//query database for items to populate
$sql = "SELECT DIS_NAME, NAME FROM INVITEM";
$query = mysqli_query($conn, $sql);
echo '<select>';
echo '<option value="">Choose your favorite fruit</option>';
while($name = mysqli_fetch_assoc($query)){
echo '<option value="' . '$name[1]' . '">' . '$name[0]' . '</option>';
}
echo '</select>';
echo $query;
?>

Should be as follows, you quoted variables, that was the problem.
echo '<option value="' . $name['DIS_NAME'] . '">' . $name['NAME'] . '</option>';
Also as RiggsFolly mentioned, you used fetch_assoc so the array keys will be named accordingly DIS_NAME and NAME.
Credit to RiggsFolly for spotting this.

In php, there are two types of quotes: single quotes and double quotes. Single quotes will not parse variables, double quotes will.
If you do want to use quotes, you could do something like this:
echo "<option value="."$name[1]".">$name[0]</option>";
So here, the double quotes will tell php to parse the variable names
However, I would recommend doing this:
echo '<option value="' . $name[1] . '">' . $name[0] . '</option>';
See this SO post for more.

Related

none value in php dropdown list

Is there any simple way to add none value in a dropdown list in php?? I tried the below but it doesn't work. It shows all lname values but not the 'None'.
I do not want to enter 1 none value in database and retrieve it. So if it is possible through php itself.
while ($row = mysqli_fetch_array($result))
{
echo "<option value=None>" . $row['lname'] . "</option> <br>";
}
Please help me.
Just add the "none"-option above the others.
echo '<option value="">None</option>';
while ($row = mysqli_fetch_array($result))
{
echo '<option value="' . $row['lname'] . '">' . $row['lname'] . '</option>';
}
Two things odd here:
- You need quotes around the None in value=None --
- a inside of a select options list isn't ever a smart thing to do.

PHP having syntax problems with quotations wrapped around a variable in PHP embedded in HTML

This appears right to me but it is incorrect (code hint coloring around '{$row["type"]}' is wrong wrong -- from the color it is in my IDE it's been considering a string, and it's throwing an error when i run it in the browser). I've spent hours trying to figure this out on my own to no avail. Any help would be greatly appreciated.
echo "<select selected = '{$row["type"]}' name='expense[" . $id . "][type]' >" . $type_options . "</select>";
When using arrays in strings, you can't use quotes. Just skip them.
echo "<select selected = '{$row[type]}' name='expense[" . $id . "][type]' >" . $type_options . "</select>";
Your quotes arren't right.
echo '<select selected ="'.$row["type"].'" name="expense['.$id.'][type]">'.$type_options.'</select>';
You currently have a problem with unescaped double quotes terminating your string. My suggestion would be to use the more standard double-quotes for your HTML element properties, and use single quotes to delineate your strings, with variable concatenated. Liek this:
echo '<select selected = "' . {$row["type"]} . '" name="expense[' . $id . '][type]">' . $type_options . '</select>';
or for even better readability, use printf
$format = '<select selected = "%s" name="expense[%d][type]">%s</select>';
printf($format, $row['type'], $id, $type_options);
$type_options = "options";
echo $type_options;
Have you not tried this?
echo "This works: {$row['type']}";
Your code wont work because you have an empty string before you echo your variable.
echo "#EMPTY STRING HERE!" . $type_options . "#EMPTY STRING HERE!";
Why are you doing that?
Use this
echo $type_options;
Or even this if you want content in your string
echo "Content" . $type_options . "Content";

PHP and OCI, Populate a drop down menu

I want to populate a html drop down menu with a list query from database. I have the following...
<?php
$conn = oci_connect(//connection stuff goes here//);
$stid = oci_parse($conn, "SELECT DESCRIPTION FROM COUNTRY WHERE COUNTRY_ID IS NOT NULL");
$result = oci_execute($stid, OCI_DEFAULT);
echo '<select>';
while ($row = oci_fetch_array($result)) {
echo '<option value=' . $row['DESCRIPTION'] . '</option>';
}
echo '</select>';
?>
Something to with the qoute marks on the echo line? Any help here would be great :)
This question has nothing to do with Oracle. Just replace this:
echo '<option value=' . $row['DESCRIPTION'] . '</option>';
... with this:
echo '<option>' . htmlspecialchars($row['DESCRIPTION']) . '</option>';
You were basically generating this:
<option value=Blah Blah Blah</option>
Edit: And you forgot about COUNTRY_ID! Add it to the SELECT statement and use it:
echo '<option value="' . htmlspecialchars($row['COUNTRY_ID']) . '">' . htmlspecialchars($row['DESCRIPTION']) . '</option>';
Yes, your assumption was right, you have to enclose your description in double quotes and also insert it inside the tags, like this:
echo '<option value="' . $row['DESCRIPTION'] . '">'.$row['DESCRIPTION'].'</option>';

options are not getting printed/echoed?

This page generates the option pulled from database . another page brings here the year of joining of students via year_joining . rest of the mysql queries works absolutely fine ( tested )
<?php
include_once("../Include/connectdb.php");
if($_GET['year_join'])
{
$id=$_GET['year_join'];
$result1 = mysql_query("select distinct sub_id from subject_profile where batch='$id'
")or die(mysql_error());
while($subid = mysql_fetch_assoc($result1)){
$result2 = mysql_query("SELECT name FROM `subjects` WHERE `sub_id` LIKE
'$subid[sub_id]'");
$subject=mysql_fetch_assoc($result2);
if($subject[name]!=""){
//print "<OPTION value=".$tmp.'">'.$tmp.'</OPTION>';
//print "<OPTION value='$tmp'>'$tmp'</OPTION>";
echo "<option value=".$subject['name'] . '">' . $subject['name'] . '</option>';
//echo '<option value="'.$id.'">'.$data.'</option>';
//}
}
}
}
?>
FYI :
//print "<OPTION value=".$tmp.'">'.$tmp.'</OPTION>';
//print "<OPTION value='$tmp'>'$tmp'</OPTION>";
echo "<option value=".$subject['name'] . '">' . $subject['name'] . '</option>';
//echo '<option value="'.$id.'">'.$data.'</option>';
none of these are working ... :(
with simple echo $tmp it works
but when ever i put as
echo "<option value=";
the result is blank page ...
and when i am echo - ing just the variable its works perfectly fine
echo $tmp;
gives the list of all the subjects ..
For the problem regarding "not displaying output", I believe you have missed a <select> tag enclosing the <option> tags.
As a side note, in the query, change it like this:
"SELECT name FROM `subjects` WHERE `sub_id` LIKE '{$subid[sub_id]}'"
or,
"SELECT name FROM `subjects` WHERE `sub_id` LIKE '" . $subid[sub_id] . "'"
Still it is not safe. You should escape the values and better use a prepared statement using mysqli or PDO
The main problem is as per Akhilesh B Chandran's answer, with a missing <select> tag.
Other than that, there will be a problem when $subject['name'] has a space in it.
The problem is this line:
echo "<option value=".$subject['name'] . '">' . $subject['name'] . '</option>';
It should be like this:
echo '<option value="'.$subject['name'] . '">' . $subject['name'] . '</option>';
In what you currently have, the browser interprets the result as this:
<option value=insert name here">insert name here</option>
As it is visible, there is a missing quote after value=.
Your quotes don't match up, which is why PHP is failing.
Try changing:
echo "<option value=".$subject['name'] . '">' . $subject['name'] . '</option>';
to:
echo "<option value=".$subject['name'] . ">" . $subject['name'] . "</option>";
Basically, you start off using a ", and switch partway through to using '

How to Populate Dropdown List's text and value from MySQL?

I am using mysqli to query a database table to obtain the value for a dropdownlist. However, I also want to retrieve the corresponding description and load that into the option text. My query is as follows:
<?php
$query=('SELECT restaurantid,location from restaurant');
$result = mysqli_query($mysqli,$query);
echo '<select name="ddlStore">';
while($row=$mysqli->fetch_array($result))
{
echo '<option value="' . htmlspecialchars($row['restaurantid']) . '">';
'</option>';
}
echo '</select>';
?>
How do I revise the above so that the location field is populating in the dropdownlist's option text?
Updated code:
<?php
$query=('SELECT restaurantid,location from restaurant');
$result = mysqli_query($mysqli,$query);
echo '<select name="ddlStore">';
while($row=$mysqli->fetch_array($result))
{
echo '<option value="' . htmlspecialchars($row['restaurantid']) . '">' .
htmlspecialchars($row['location']) .
'</option>';
}
echo '</select>';
?>
The above still does not display the locations as dropdownlist option text values.
Update #2:
When viewing the page, the dropdownlist doesn't show the values from the database table. Using IE Developer Tools, I see a script error in the while statement:
Call to undefined method mysqli::fetch_array()
Is there a more optimal way to structure the mysqli_fetch_array statement?
Change to...
echo '<option value="' . htmlspecialchars($row['restaurantid']) . '">' . htmlspecialchars($row['location']) . '</option>';
echo '<option value="' . htmlspecialchars($row['restaurantid']) . '">' . htmlspecialchars($row['location']) . '</option>';
But what have you tried so far?
Try this way (more info here)
$result = $mysqli->query($query)
while($row = $result->fetch_assoc()) {
}

Categories