This question already has answers here:
Using PHP to populate a <select></select> dropdown? [duplicate]
(7 answers)
Closed 7 months ago.
Hopefully this question doesn't drive anyone to drink.
I've been at this for about 9 hours now and cannot get a drop down to populate. I know I'm missing something very simple and I'm reaching out to see if anyone can give me some insight. The table I'm calling from has 1 column and just has names (which are unique)
I have a db class being called in the beginning of all of my pages, in all but 2 cases , its working fine, in the 2 exceptions, it's the pages that require the dropdown. I've deleted and recreated both pages with no change.
Both Pages are named select.php and select_p.php. The initial call is as follows and breaks at the first "->" so begins printing everything after it and up until the "?>"
include("database.class.php");
$database = new database;
$sql_page = $database->mysqlQuery("SELECT * FROM spec_tables");
$edata_page = $database->mysqlFetchArray($sql_page);
return $edata_page;
?>
Heres the fucntion in the class file that works on 2 other sites and other pages in this same site
function mysqlQuery($qry)
{
$rs = mysql_query($qry, $this->DatabaseLink);
return $rs;
echo mysql_error();
}
Now if I use the code in the page, it doesnt print but the dropdown is blank
<select name='list' value=''><option>Select List</option>
<?
$sql_page = $database->mysqlQuery("SELECT * FROM spec_tables");
$edata_page = $database->mysqlFetchArray($sql_page);
return $edata_page;
foreach($edata_page as $row){
?>
<option value="<?php echo $row; ?>"><?php echo $row; ?></option>
<? } ?>
</select>
I've looked at the following (plus about 20 pages that are not this close)
PHP- Fetch from database and store in drop down menu html
How to put table value in a dropdown menu with MYSQL and PHP
http://www.plus2net.com/php_tutorial/list-table.php
http://www.tutorialrepublic.com/faq/how-to-populate-dropdown-list-with-array-values-in-php.php
Here's all the snippets of code I've tried to no avail, some of them actually print the code in the html form, any constructive help would be very much appreciated.
*********************************************************
<select name='list' value=''><option>Select List</option>
<?
$sql_page = $database->mysqlQuery("SELECT * FROM spec_tables");
$edata_page = $database->mysqlFetchArray($sql_page);
return $edata_page;
foreach($edata_page as $row){
?>
<option value="<?php echo $row; ?>"><?php echo $row; ?></option>
<? } ?>
</select>
*****************************************************************
<select name='list' value=''><option>Select List</option>
<?
$result = $database->mysqlQuery("select * from spec_tables");
if (!$result) die('Couldn\'t fetch records');
$num_fields = mysql_num_fields($result);
$row = array();
for ($i = 0; $i < $num_fields; $i++)
while ($row = mysql_fetch_row($result))
{
?>
<option value="<?php echo $row; ?>"><?php echo $row; ?></option>
<? } ?>
</select>
********************************************************************
<select name='list' value=''><option>Select List</option>
<?
$result = $database->mysqlQuery("select * from spec_tables");
if(mysql_num_rows($result) > 0){
while($row = mysql_fetch_array($result)) {
echo '<option value=". $row['name'] .">' . $row['name'] . '</option>';
}
}
?>
</select>
****************************************************************************
Placed in top of file
$servername = "localhost";
$username = "uname";
$password = "pword";
$dbname = "db_name";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
if ($result = mysqli->query("SELECT * FROM 'spec_tables'")) {
printf("Select returned %d rows.\n", $result->num_rows);
/* free result set */
$result->close();
}
**********************************************************************
<?
$sql = "select * from spec_tables";
$result = mysql_query($sql);
echo "<select name='list' value=''><option>Select List</option>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['name'] ."'>" . $row['name'] ."</option>";
}
echo "</select>";
?>
**************************************************
Heres the code thats working
//placed in beginning of code
<?php
include("database.class.php");
$database = new Database;
$result = $database->mysqlQuery("SELECT * FROM spec_tables");
?>
//Placed inside html form
<?php
echo '<select name="list">';
echo '<option>Select List</option>';
while ($row = mysql_fetch_array($result)) {
echo '<option value="' . $row['name'] .'">' . $row['name'] .'</option>';
}
echo '</select>';
?>
Related
This question already has answers here:
Using PHP to populate a <select></select> dropdown? [duplicate]
(7 answers)
Closed 7 months ago.
am trying to populate a dropdown list from mysql database table
here is the code
<div class="form-group">
Select Make
<?php
include '../db_config/dbcon.php';
$sql = "SELECT * FROM vehicle_details";
$result = mysql_query($sql);
echo "<select name='vehicle_make'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['vehicle_make'] . "'>" . $row['vehicle_make'] . "</option>";
}
echo "</select>";
?>
</div>
This is what is displayed by the code
Where am i going wrong??
This worked too, its a modification of janlindo's above
<div class="form-group">
Select Make <?php
include '../db_config/dbcon.php';
$sql = "SELECT * FROM vehicle_details";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<select name='vehicle_make'>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<option value='" . $row['vehicle_make'] . "'>" . $row['vehicle_make'] . "</option>";
}
echo "</select>";
}
?>
</div>
Depending on whats in your dbcon.php, but here's an example using mysqli_query:
<div class="form-group">
Select Make <?php
// start of dbcon
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
$conn = new mysqli($servername, $username, $password, $dbname);
//end of dbcon
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM vehicle_details";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<select name='vehicle_make'>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<option value='" . $row['vehicle_make'] . "'>" . $row['vehicle_make'] . "</option>";
}
echo "</select>";
}
$conn->close();
?>
</div>
I am struggling to get this code to work, what I want from it is to show the item (that is already in the database) to be selected in the selection form.
<label>Server Ports:</label>
<select multiple class="form-control" name="select[]">
<?php
// Get Server Information
$query = "SELECT port_no FROM _servers WHERE (server_id = '$servid') ";
$result = mysql_query($query) or die ('Unable to run query:'.mysql_error());
while($row = mysql_fetch_assoc($result)){
$no = $row['port_no'];
}
$query = "SELECT id, name, port_no, unique_id FROM ports ORDER BY name ASC";
$result = mysql_query($query) or die ('Unable to run query:'.mysql_error());
while($row = mysql_fetch_assoc($result))
{
$port_no = $row['port_no'];
$port_name = $row['name'];
$p_unique = $row['unique_id'];
}
?>
<option value="<?php echo $p_unique;?>"
<?php
if ($p_unique == $no) {
$check = 'selected';
} else {
$check = '';
}
echo $check;
?>
>
<?php
echo $row['name'];
?>
(
<?php
echo $row['port_no'];
?>
)</option>
<?php
}
?>
</select>
Are you sure your test must be if ($p_unique == $no) {} and not if ($port_no == $no) {} ?
If yes, try to checks your variables values :
Do a var_dump() of $no :
var_dump($no);
In your while() loop, you can also check values of $p_unique and $no like that :
var_dump($p_unique.'/'.$no);
Also, here is a simplest way to test for selected :
<option value="<?php echo $p_unique;?>" <?php if ($p_unique == $no) echo 'selected="selected"'; ?>><?php echo $row['name'] ;?> (<?php echo $row['port_no'] ;?>)</option>
I'm working on a form that has 4 different select elements from 2 tables of a database. I haven't done anything like this and I don't really know how to do it.
I have a table called "students" from I need "name" and "class" and a table called "books" from I need "writer" "title" ... all is one select element and all has more than 2 option values.
I've tried with only one sql query and one select but it shows only one option on the site, wether it has about 6 values in the database.
My code:
$sql = "SELECT class
FROM students";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
$select_class = "<option value={$row['class']}>{$row['class']}</option>";
}
<select id="class" name="class">
<?php print $select_class; ?>
</select>
How would it be correct?
try this
$sql = "SELECT class FROM students";
$result = mysql_query($sql);
echo '<select id="class" name="class">';
while ($row = mysql_fetch_assoc($result)) {
echo "<option value={$row['class']}>{$row['class']}</option>";
}
echo '</select>';
You are overwriting $select_class on each while() loop. You need to concatenate $select_class . Change to $select_class .=
$select_class = "";
while ($row = mysql_fetch_assoc($result)) {
$select_class .= "<option value={$row['class']}>{$row['class']}</option>";
}
Changing this:
$select_class = "<option value={$row['class']}>{$row['class']}</option>";
to this:
$select_class .= "<option value={$row['class']}>{$row['class']}</option>";
might solve your problem.
Right now you are constantly resetting the value of $select_class, instead of adding to it. The .= assignment should help you get around this.
As always, be sure to up-vote any StackOverflow answers you find useful.
Try this
<?php
$dbhandle = mysql_connect($hostname, $username, $password) or die("can't connect");
$table = "students";
$sql = "SELECT * FROM students";
$result = mysql_query($sql, $dbhandle);
mysql_data_seek($result, 0);
?>
<form>
<select class="dropdown" name="dropdown">
<?php
if(mysql_num_rows($result) > 0){
while($row = mysql_fetch_array($result)) {
echo '<option value="' . $row['class'] . '">' . $row['class'] . '</option>';
}
}
?>
</select>
</form>
I am new to php, i created drop down which calling data from mysql data base, user selects option and its save to data base.
Problem Arises in edit form in which its do not showing selected value.
Drop Down code is below:
$query = 'SELECT name FROM owner';
$result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
//create selection list
echo "<select name='owner'>\name";
while($row = mysql_fetch_row($result))
{
$heading = $row[0];
echo "<option value='$heading'>$heading\n";
}
echo "</select>"
Please advise solution for the edit form.
Thanks in Advance
you must close <option> tag:
echo "<option value='$heading'>$heading</option>";
$query = 'SELECT name FROM owner';
$result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
//create selection list
echo "<select name='owner'>\name";
while($row = mysql_fetch_row($result))
{
$heading = $row[0];
?>
<option <?php if($heading=="SOMETHING") { echo "selected='selected'"; } ?> value="SOMETHING">SOMETHING</option>
<option <?php if($heading=="SOMETHING2") { echo "selected='selected'"; } ?> value="SOMETHING2">SOMETHING2</option>
<option <?php if($heading=="SOMETHING3") { echo "selected='selected'"; } ?> value="SOMETHING3">SOMETHING3</option>
<?php
}
echo "</select>"
I'd do it this way.
$numrows = mysql_num_rows($result);
if ($numrows != 0){
echo "<select name='owner'>\name";
while ($x = mysql_fetch_assoc($result)){
echo "<option value='".$x['heading']."'>".$x['heading']."</option>";
}
echo "</select>";
}
$x['heading'] is using the value of the row 'heading' in the database
It's much more efficient and simply looks more sophisticated.
I am trying to populate a Drop down box from results of a mySQL Query, in Php. I've looked up examples online and I've tried them on my webpage, but for some reason they just don't populate my drop down box at all. I've tried to debug the code, but on the websites I looked at it wasn't really explained, and I couldn't figure out what each line of code. Any help would be great :)
Here's my Query: Select PcID from PC;
You will need to make sure that if you're using a test environment like WAMP set your username as root.
Here is an example which connects to a MySQL database, issues your query, and outputs <option> tags for a <select> box from each row in the table.
<?php
mysql_connect('hostname', 'username', 'password');
mysql_select_db('database-name');
$sql = "SELECT PcID FROM PC";
$result = mysql_query($sql);
echo "<select name='PcID'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['PcID'] . "'>" . $row['PcID'] . "</option>";
}
echo "</select>";
?>
Below is the code for drop down using MySql and PHP:
<?
$sql="Select PcID from PC"
$q=mysql_query($sql)
echo "<select name=\"pcid\">";
echo "<option size =30 ></option>";
while($row = mysql_fetch_array($q))
{
echo "<option value='".$row['PcID']."'>".$row['PcID']."</option>";
}
echo "</select>";
?>
Since mysql_connect has been deprecated, connect and query instead with mysqli:
$mysqli = new mysqli("hostname","username","password","database_name");
$sqlSelect="SELECT your_fieldname FROM your_table";
$result = $mysqli -> query ($sqlSelect);
And then, if you have more than one option list with the same values on the same page, put the values in an array:
while ($row = mysqli_fetch_array($result)) {
$rows[] = $row;
}
And then you can loop the array multiple times on the same page:
foreach ($rows as $row) {
print "<option value='" . $row['your_fieldname'] . "'>" . $row['your_fieldname'] . "</option>";
}
No need to do this:
while ($row = mysqli_fetch_array($result)) {
$rows[] = $row;
}
You can directly do this:
while ($row = mysqli_fetch_array($result)) {
echo "<option value='" . $row['value'] . "'>" . $row['value'] . "</option>";
}
At the top first set up database connection as follow:
<?php
$mysqli = new mysqli("localhost", "username", "password", "database") or die($this->mysqli->error);
$query= $mysqli->query("SELECT PcID from PC");
?>
Then include the following code in HTML inside form
<select name="selected_pcid" id='selected_pcid'>
<?php
while ($rows = $query->fetch_array(MYSQLI_ASSOC)) {
$value= $rows['id'];
?>
<option value="<?= $value?>"><?= $value?></option>
<?php } ?>
</select>
However, if you are using materialize css or any other out of the box css, make sure that select field is not hidden or disabled.
After a while of research and disappointments....I was able to make this up
<?php $conn = new mysqli('hostname', 'username', 'password','dbname') or die ('Cannot connect to db') $result = $conn->query("select * from table");?>
//insert the below code in the body
<table id="myTable"> <tr class="header"> <th style="width:20%;">Name</th>
<th style="width:20%;">Email</th>
<th style="width:10%;">City/ Region</th>
<th style="width:30%;">Details</th>
</tr>
<?php
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>".$row['username']."</td>";
echo "<td>".$row['city']."</td>";
echo "<td>".$row['details']."</td>";
echo "</tr>";
}
?>
</table>
Trust me it works :)
What if you want to use both id and name in the dropdown? Here is the code for that:
$mysqli = new mysqli($servername, $username, $password, $dbname);
$sqlSelect = "SELECT BrandID, BrandName FROM BrandMaster";
$result = $mysqli -> query ($sqlSelect);
echo "<select id='brandId' name='brandName'>";
while ($row = mysqli_fetch_array($result)) {
unset($id, $name);
$id = $row['BrandID'];
$name = $row['BrandName'];
echo '<option value="'.$id.'">'.$name.'</option>';
}
echo "</select>";