I begin to learn PHP because VBA is not enough.
And today I ask you to help.
I have Select box, which takes data from MySQL.
I have Input to which I would like to enter the data from the second column, the same table with that take the data to a select box and do not know how I do it.
If you want to add value from variable to input field you can do next
//$var you get from query from this your mysql table
<input type="text" name="somename" value="<?php echo $var['second_column']; ?>" id="someid">
are you asking for how to put data into a selectbox from database ?? If so, you can use this..
<select name="country">
<option value="">Select</option>
<?php
$countriesQuery=mysqli_query($conn,"SELECT id,name FROM countries");
while($countries=mysqli_fetch_assoc($countriesQuery))
{
echo "<option value='$countries[id]'>$countries[name]</option>";
}
?>
</select>
<select name="country">
<option value="">Select</option>
<?php
$countriesQuery=mysqli_query($conn,"SELECT id,name FROM countries");
while($countries=mysqli_fetch_assoc($countriesQuery))
{
echo "<option value='".$countries[id]."'>".$countries[name]." </option>";
}
?>
</select>
Hope this helps
<?php
$mysqli= new mysqli("localhost", "root", "", "db");
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
$result=$mysqli->query("select name from table");
/* name is your second field*/
?>
<select name="">
<?php
while($res=$result->fetch_array())
{?>
<option value="<?php echo $res[0]; ?>"><?php echo $res[0]; ?></option>
<?php
}
?>
</select>
Related
I have created function, which retrieve the data from database and put it into dropdown menu. That step works fine.
Here I get dropdown menu with all needed values.
function termopaneli() {
$link = new mysqli("localhost", "xxx", "xxxx", "xxx");
$link->set_charset("utf8");
$sql=mysqli_query($link, "SELECT * FROM termopaneli order by PaneliId asc");
echo '<option value="">Izaberi panel</option>';
while($record=mysqli_fetch_array($sql)){
echo '<option value= "' .$record['PaneliId']. '">' . $record['PaneliNaziv'] . ' </option>';
}
}
Later on when I want to see what is chosen inside dropdown, it does not print. I am using this form to edit, so selected value is important.
First I get values from database. Everything works except for printing selected value in dropdown.
First I get $panel value and when I echo $panel, I get value I should. But it does not show up as selected option.
<label>Panel</label>
<select class="form-control" name="panel" value="<?php echo $panel; selected" ?>">
<option value=<?php echo $panel?> selected>
<?php termopaneli()?>
</option>
</select>
Any help or advice is appreciated.
Looks like </option> should come before your PHP function call.
Also, check the value in <select> is correct?
<label>Panel</label>
<select class="form-control" name="panel" value="<?php echo $panel; ?>">
<option value=<?php echo $panel?> selected> SELECTED_OPTION </option>
<?php termopaneli()?>
</select>
Suppose at time of edit u have selected value like $select=5
<label>Panel</label>
<select class="form-control" name="panel" value="<?php echo $panel; selected" ?>">
<option value=<?php echo $panel?> selected>
<?php termopaneli($select)?>
</option>
</select>
function termopaneli($select) {
$link = new mysqli("localhost", "xxx", "xxxx", "xxx");
$link->set_charset("utf8");
$sql=mysqli_query($link, "SELECT * FROM termopaneli order by PaneliId asc");
echo '<option value="">Izaberi panel</option>';
while($record=mysqli_fetch_array($sql)){
echo '<option value= "' .$record['PaneliId']. '"; if($record['PaneliID']==$select){echo 'selected=selected'}>' . $record['PaneliNaziv'] . ' </option>';
}
}
My HTML code is here.how to fetch my this data from database?
<tr>
<td>City:
<select name="city">
<option selected="selected">--Select City--</option>
<option value="<?php echo $row['city'];?>">Ahmedabad</option>
<option value="<?php echo $row['city'];?>"> Vadodara</option>
<option value="<?php echo $row['city'];?>"> Rajkot</option>
<option value="<?php echo $row['city'];?>"> Surat</option>
</select><br />
</td>
Assuming you have created the database and connected it,
you can try something like this:
<?php
$query = mysqli_query("YOUR QUERY HERE"); // Run your query
echo '<select name="DROP DOWN NAME">'; // Open your drop down box
// Loop through the query results, outputing the options one by one
while ($row = mysqli_fetch_array($query)) {
echo '<option value="'.$row['something'].'">'.$row['something'].'</option>';
}
echo '</select>';// Close your drop down box
?>
Here is the information about mysqli_query and mysqli_fetch_array.
I know this post is little old; however, sharing my answer for this will help in some ways whoever sees this post.
Below answer worked for me which is inspired from the above verified answer with some corrections.
<?php
$con=mysqli_connect("localhost","your_db_username_here","your_db_password_here","your_db_name");
$query=mysqli_query($con,"SELECT column_name FROM table_name");
echo '<select name="NameHere">';
while ($row = mysqli_fetch_array($query)) {
echo '<option>'.$row['column_name'].'</option>';
}
echo '</select>';
?>
I'm trying to populate a dropdown field inside a html form, but the field doesn't show any value, only a blank one.
I'm trying the following code:
<select name="Select" class="textfields" id="prods">
<option id="0">--Producto--</option>
<?php
require("conectdb.php");
$allproducts = mysql_query("SELECT * FROM Productos");
while ($viewallproducts = mysql_fetch_array($allproducts)){
?>
<option id="<?php echo $viewallproducts["ID"];?>"><?php echo $viewallproducts["CODIGO"];?></option>
<?php } ?>
</select>
I have changed the quotes and ;... But still nothing, here is the code of connection to the Database (conectdb.php):
<?php
$con=mysqli_connect("localhost","xxxxxx","xxxxxx","xxxxxx");
// Check connection
if (mysqli_connect_errno())
echo "Failed to connect to MySQL: " . mysqli_connect_errno();
mysqli_close($con);
?>
It seems to be a problem of the database connection, Im trying now the following code to see what happens:
<?php
$username = "xxxxx";
$password = "xxxxx";
$hostname = "xxxx";
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
$allproducts = mysql_query("SELECT * FROM PRODUCTOS");
while ($viewallproducts = mysql_fetch_array($allproducts)){
echo ($viewallproducts);
}
?>
But, on this php, I receive the following error:
Warning: mysql:fetch_array() expects parameter 1 to be resource.
I have also tried with mysql_fetch_row() and gives me the same error.
1) You have error in require line(put file name inside quotation )
2) you don't put value attribute in option.
Try this:
<select name="Select" class="textfields" id="prods">
<option id="0" value="o">--Producto--</option>
<?php
require("conectdb.php");
$allproducts = mysql_query("SELECT * FROM Productos");
while ($viewallproducts = mysql_fetch_array($allproducts)){
?>
<option id="<?php echo $viewallproducts['ID'];?>" value="<?php echo $viewallproducts['ID'];?>"><?php echo $viewallproducts['DESCRIPCION'];?></option>
<?php } ?>
</select>
I think you made a mistake because you think ID in html is ID in database. You need to use "value" attribute for option
chance this line
<option id="<?php echo $viewallproducts['ID'];?>"><?php echo $viewallproducts['DESCRIPCION'];?></option>
to
<option value="<?php echo $viewallproducts['ID'];?>"><?php echo $viewallproducts['DESCRIPCION'];?></option>
You have also forget about:
require argument in ''
; after require
Function to get rows should be 'mysql_fetch_row' instead of 'mysql_fetch_array'
I would also suggest you using better DB engine for example PDO or MySQLi because MySQL_* functions are depracated.
Code formatted:
<select name="Select" class="textfields" id="prods">
<option value="0">--Producto--</option>
<?php
require('conectdb.php'); // be sure that file exist you forgot about '' and ;
$allproducts = mysql_query("SELECT * FROM `Productos`");
while ($viewallproducts = mysql_fetch_row($allproducts))
echo '<option value="'.$viewallproducts['ID'].'">'.$viewallproducts['DESCRIPCION'].'</option>';
?>
</select>
You forget to put the semicolon after the line
require(conectdb.php)
And you should add a quotation
require("conectdb.php");
Maybe is that
Some edit on your code:
<select name="Select" class="textfields" id="prods">
<option id="0">--Producto--</option>
<?
require("conectdb.php");
$allproducts = mysql_query("SELECT * FROM Productos")
or die(mysql_error());
while ($viewallproducts = mysql_fetch_assoc($allproducts)){
?>
<option id="<?=$viewallproducts['ID'];?>" value="<?=$viewallproducts['ID'];?>">
<?=$viewallproducts['DESCRIPTION'];?></option>
<?php } ?>
</select>
You should use mysql_fetch_assoc instead of mysql_fetch_array. You should also check if the sql query succeed first.
I can see the query returning results, but I can't seem to be able to put them into a html dropdown box. Also, the dropdown box has just as many entries as the query returns, but THEY ARE ALL WHITE SPACES. HOWEVER, the page source shows correct option values such as
<option value="3 John"></option>
<option value="Jude"></option>
<option value="Revelation"></option>
Can somebody help me out? Why dont they actually show in the dropdown box?
<html>
<?php
//Connect to the database
$mysqli = new mysqli("localhost", "root", "", "bible");
//Return an error if we have a connection issue
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
//Query the database for the results we want
$query = $mysqli->query("select distinct bname as Name from kjv limit 1");
//Create an array of objects for each returned row
while($array[] = $query->fetch_object());
array_pop($array);
//Print out the array results
print_r($array);
?>
<h3>Dropdown Demo Starts Here</h3>
<select name="the_name">
<?php foreach($array as $option) : ?>
<option value="<?php echo $option->Name; ?>"></option>
</select>
<?php endforeach; ?>
Try This
<select name="the_name">
<?php foreach($array as $option) : ?>
<option value="<?php echo $option['Name']; ?>"><?php echo $option['Name']; ?></option>
<?php endforeach; ?>
</select>
After the query is executed use the while loop to add the options to select
$query = $mysqli->query("select distinct bname as Name from kjv limit 1"); ?>
<select>
<?php while($option = $query->fetch_object()){ ?>
<option><?php echo $option->Name; ?></option>
<?php } ?>
</select>
Not sure what the array_pop is doing in the code
AS TIM WAX SAID THIS IS THE SOLUTION
$query = $mysqli->query("select distinct bname as Name from kjv limit 1"); ?>
<select>
<?php while($option = $query->fetch_object()){ ?>
<option><?php echo $option->Name; ?></option>
<?php } ?>
</select>
<select name="the_name">
<?php foreach($array as $option) : ?>
<option value="<?php echo $option->Name; ?>"></option>
<?php endforeach; ?>
</select>
You ended your loop in a way that it also create <select> tag again and again. Change it and try again. I don't know much about .php but it could be a problem in showing your dropdown box.
here is mine .. im a beginner but it works for me,
$query = $mysqli->query("SELECT * FROM `student_type_db`"); //table of student type
echo "<select>";
while($row = $query->fetch_array()){
echo "<option>";
echo $row['student_type'] . " - " . $row['student_description'];
echo "</option>";
}
echo "</select>";
// student type = 1 | student description = regular
// output : 1 - regular
I want my program to list my data from mysql table in a list in a dropdown menu on my page.
Here's my code:
<fieldset>
<legend> Selecteer uw Categorie </legend>
<label for ="Categorie"> Categorie </label>
<select name ="Categorie" id="Categorie">
<datalist id ="Categorie">
<Option Value="Router">Router</option>
<Option Value="Switch">Switch</option>
<Option Value="Toestel">Toestel</option>
<Option Value="Basisstation">Basisstation</option>
<Option Value="Repeaters">Repeaters</option>
<Option Value= <?php
$con=mysqli_connect("localhost","root","admin","inventarisdb");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Categorien");
while($row = mysqli_fetch_array($result))
{
echo "<ul>";
echo "<li>" . $row['Categorieen1'] . "</li>";
echo "</ul>";
}
echo "</table>";
mysqli_close($con);
?>
</option>
</select>
</datalist>
</fieldset>
THis code works perfectly, It looks up the data i need and posts it in the dropdown list But it all gets posted in a single line..
I want it to be Listed underneath each other..
Please help me!
They are all on a single line because you put your result in a single <option> tag.
try this:
<Option Value="Basisstation">Basisstation</option>
<Option Value="Repeaters">Repeaters</option>
<?php
$con=mysqli_connect("localhost","root","admin","inventarisdb");
// Check connection
if (mysqli_connect_errno())
{
echo "<option>Failed to connect to MySQL: " . mysqli_connect_error()."</option>";
}
$result = mysqli_query($con,"SELECT * FROM Categorien");
while($row = mysqli_fetch_array($result))
{
echo "<option>".$row['Categorieen1'] . "</option>";
}
mysqli_close($con);
?>
</select>
</datalist>
</fieldset>
EDIT:
sorry for the mistake I forgot the second echo in the while loop XP!