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>
Related
I am trying to do a simple dropdown list on a table, however, my codes don't seem to be working, I was wondering if there any issue with the way I connect and retrieve? or its just my codes for the dropdown list is wrong. Here are the codes for it, below screenshot contains my database along with the place I want to put my dropdown list. Thanks for the time.
<?php
$mysqli = new mysqli(spf, dbuser, dbpw, db);
$stmt = $mysqli->prepare("Select sbranch_name from branches");
$result = $stmt->execute();
$stmt->bind_result($sbranch_name);
//while ($stmt->fetch())
//{
// $stmt .="<option>". $row['sbranch_name']. "</option>";
//echo '<input type="checkbox" name="sbranch_name[]" value="'.$sbranch_name.'". <br>';
// echo $stmt;
//}
if ($result->num_rows > 0) {
echo "<select name='sbranch_name'>";
while($row = $result->fetch_assoc()) {
echo "<option value='" . $row['sbranch_name'] . "'>" . $row['sbranch_name'] . "</option>";
}
echo "</select>";
}
$stmt->close();
$mysqli->close();
?>
try this code
using mysqli->query
<?php
$mysqli = new mysqli(spf, dbuser, dbpw, db);
$sql="Select sbranch_name from branches";
$result = $mysqli->query($sql);
//$stmt->bind_result($sbranch_name);
//while ($stmt->fetch())
//{
// $stmt .="<option>". $row['sbranch_name']. "</option>";
//echo '<input type="checkbox" name="sbranch_name[]" value="'.$sbranch_name.'". <br>';
// echo $stmt;
//}
if ($result->num_rows > 0) {
echo "<select name='sbranch_name'>";
while($row = $result->fetch_assoc()) {
echo "<option value='" . $row['sbranch_name'] . "'>" . $row['sbranch_name'] . "</option>";
}
echo "</select>";
}
//$stmt->close();
$mysqli->close();
?>
I think the issue in when you binding query, why dont you use below code.
$result = $mysqli->query("Select sbranch_name from branches");
if ($result->num_rows > 0) {
echo "<select name='images'>";
while($row = $result->fetch_assoc()) {
echo "<option value='" . $row['sbranch_name'] . "'>" . $row['sbranch_name'] . "</option>";
}
echo "</select>";
}
If there's duplicated topic - please, forgive me, but i haven't found a solution yet. First of all, I've got one php file (update.php). This file contains:
<form action='update.php' method='post'>
<table border='1'>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "exchange";
$conn = new mysqli($servername, $username, $password, $dbname);
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_set_charset($conn,"utf8");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT ID, name, symbol, buy, sell FROM exchangevalues";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td><input type='text' name='ID' value='".$row["ID"]."'>";
echo "<td>".$row["name"]."</td>";
echo "<td>Buy: <input type='text' name='buy' value='".$row['buy']."'></td>";
echo "<td>Sell: <input type='text' name='sell' value='".$row['sell']."'></td>";
echo "</tr>";
}
}
$conn->close();
?>
<table>
There's 14 rows and 5 columns in my db. How do i update every row in columns 'buy' and 'sell' on 'click' (isset submit button) with new values? I've tried with ID[], buy[], but I've got problems with loop and i cannot handle it. I'm running MySQL on localhost, also i know that only last row will be updated without running loop, but still...
I am assuming you have corresponding php code to process the input, here is a sample of what you'll need to do for the HTML when creating the form:
if ($result->num_rows > 0) {
$i=0;
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td><input type='text' name='ID[" . $i . "]' value='".$row["ID"]."'>";
echo "<td>".$row["name"]."</td>";
echo "</tr>";
}
}
On the php side to process the input, you can do something like this:
$id_array = $_REQUEST["ID"];
foreach($id_array as $id){
//do insert with $id
}
Sorry for asking a duplicate question. I tried few ways given on this website for fetching data. But any of them did not work. I'll put my coding down below.
<?php
mysql_connect('localserver', 'root', '123');
mysql_select_db('database');
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} else {
echo "Connected successfully";
$sql = "SELECT name FROM company";
$result = mysql_query($sql);
echo "<select name='name'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['name'] ."'>" . $row['name'] ."</option>";
}
echo "</select>";
}
?>
Any help would be greatly appreciated.
I think you could use this code.
Make sure you have the correct credentials.
establish your DB connection :
<?php
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "db";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "SELECT name FROM company";
$result = $conn->query($sql);
$conn->close();?>
Then to view the results in your html:
<select name="name">
<?php
while($row = $result->fetch_assoc()) {
echo "<option value='" . $row['name'] ."'>" . $row['name'] ."</option>";
}
?>
</select>
I think this would do the trick.
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>';
?>
I am trying to use PHP in two different parts of a page. But when I try to load the page I receive the error: undefined variable $conn. Why am I getting this error?
<tbody>
<?php
$sql = "SELECT id, name, price FROM periperichicken";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . $row["name"] . "</td>";
echo "<td>£" . $row["price"] . "</td>";
echo "<td><input type=\"text\" name=\"amount\"
class=\"amount-type\"
placeholder=\"Amount\"/></td>";
echo "<td>Add to cart</td>";
echo "</tr>";
}
}
mysqli_close($conn);
?>
</tbody>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "pizza";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
?>
You use $conn at the top of your script
$result = mysqli_query($conn, $sql);
Yet you define it further down
$conn = mysqli_connect($servername, $username, $password, $dbname);
You'll need to move that section up to the first before you can run any queries.
<?PHP
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "pizza";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
?>
<tbody>
<?php
$sql = "SELECT id, name, price FROM periperichicken";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . $row["name"] . "</td>";
echo "<td>£" . $row["price"] . "</td>";
echo "<td><input type=\"text\" name=\"amount\" class=\"amount-type\" placeholder=\"Amount\"/></td>";
echo "<td>Add to cart</td>";
echo "</tr>";
}
}
mysqli_close($conn);
?>
</tbody>
You're defining and connecting to the database after you try to use it. Move the connection part above the query. If this is the only place that you will be using the connection the page then this is fine. But if other pages or sections of the page will use the connection you should look at placing this in a common file that is included at the top of every page that uses it.
<tbody>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "pizza";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT id, name, price FROM periperichicken";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . $row["name"] . "</td>";
echo "<td>£" . $row["price"] . "</td>";
echo "<td><input type=\"text\" name=\"amount\" class=\"amount-type\" placeholder=\"Amount\"/></td>";
echo "<td>Add to cart</td>";
echo "</tr>";
}
}
mysqli_close($conn);
?>
</tbody>
I'd be pretty sure that you need to define $conn before you call it in the top part of the php code.
PHP is loaded and executed sequentially... You are defining $conn after using it. Try doing it before. I.e.: Move the second blob of PHP above the first one.
For more on PHP execution order, check out this question.