I have created one IMS in that I am trying to fetch data from one table from the database and display it in drop-down form to another page.
In the database, I have created one table named a party in that table one column named party_name is available and I have to fetch that column data to my order page. Below is my Code. If anyone knows a solution than please help.
<select name="party[]" style="width:100%;" required>
<?php
$result=mysql_query("SELECT * FROM partys");
while ($row = mysql_fetch_array($result)) {
?>
<option value="<?php echo $row['id'];?>"><?php echo $row['party_name'];?></option>
<?php
}
?>
</select>
Firstly: mysql extension is deprecated, you should use at least mysqli_*:
Secondly: try the below example, replacing the database connection string variables with your database credentials:
<select name="party[]" style="width:100%;" required>
<option value="">-- Please select --</option>
<?php
$dbc = mysqli_connect('localhost', 'userName', 'password', 'databaseName')
or die('Error connecting to MySQL server.');
$query = "SELECT * FROM partys";
$result = mysqli_query($dbc, $query);
while ($row = mysqli_fetch_array($result)) {
?>
<option value="<?php echo $row['id'];?>"><?php echo $row['party_name'];?></option>
<?php } ?>
</select>
Related
I have been trying to populate a drop down menu by pulling the needed information from a database through php and mysql however I haven't been able to achieve this and so far when I run it I just get a drop down box with nothing inside of it. Any help on this would be great as I am really stuck as to where I am going wrong.
<select id="dung-name-select" name="dung-select">
<option name="" disabled selected hidden>Name</option>
<option value="*">All</option>
<?php
//Database Query
$query = "SELECT Name FROM dungeon";
$result = mysqli_query($connection, $query);
if (!$result) {
die("Database query failed.");
}
var_dump($result);
while($row = mysqli_fetch_assoc($result)) {
?>
<option value="<?= $row['Name']; ?>"><?= $row['Name']; ?></option>;
<?php
}
?>
</select>
I have connected to the database fine and can pull information down from it to populate a table for example however the drop down menu just isn't working.
NB: I need the value to be set to the name that I am also populating the drop down menu with.
your code works for me, i tried something different hope it works for you.
<select id="dung-name-select" name="dung-select">
<option name="" disabled selected hidden>Name</option>
<option value="*">All</option>
<?php
$result = $connection->query("SELECT Name FROM dungeon");
if ($result) {
while($row = $result->fetch_assoc()) {
?>
<option value="<?= $row['Name']; ?>"> <?= $row['Name']; ?></option>;
<?php
}
}
else {
echo "No dungeons found";
}
?>
</select>
I am trying to understand the code and I am confused to understand the code at this point below. in the first example I want to fetch data from the database and display them as drop-down list but unfortunately it is not displaying anything, and in the second example everything is working fine (fetching data displaying as the drop-down list) bu the problem is that I cant understand the code in the second example it looks much more complicated than the first example. So I want to correct my first example and after that I can rebuilt the second example based on the first one.
First table is department (ID, persondepartment, Description)
Second table board ( personboard, description)
ps: this is for my project so I dont care about Security
First Example - Not Working
<td>Department</td>
<td>
<select name='persondepartment' ID="dept">
<option ID="0">-- Select Department --</option>
<?php
mysql_connect('localhost', 'data_datab', 'password');
mysql_select_db ("data");
$getalldepartments = "SELECT * FROM department";
WHILE ($viewdepartments = mysql_fetch_array(
$getalldepartments)){
?>
<option ID="<?php echo $viewdepartments['ID'];?>
"> <?php echo $viewdepartments ['persondepartment'] ?></option>
<?php } ?>
</select>
</td>
Second Example - Working
<td>Board</td>
<td>
<?php
mysql_connect('localhost', 'data_datab', 'password');
mysql_select_db ("data");
$sql = "SELECT * FROM board";
$result = mysql_query($sql);
echo "<select name='personboard'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['personboard'] . "'>" . $row['personboard'] . "</option>";
}
?>
</td>
Try to change
$getalldepartments = "SELECT * FROM department";
to:
$getalldepartments = mysql_query("SELECT * FROM department");
and consider to use mysqli instead of deprecated mysql extension.
Correct version with PDO
<td>Department</td>
<td>
<select name="persondepartment" id="dept">
<option value="0">-- Select Department --</option>
<?php
try {
$pdo = new PDO('mysql:host=localhost;dbname=data', 'data_datab', 'password');
} catch (PDOException $e) {
echo '<option value="">' . $e->getMessage() . '</option>';
}
$getalldepartments = $pdo->query("SELECT * FROM department");
while ($viewdepartments = $getalldepartments->fetch(PDO::FETCH_ASSOC)) {
?>
<option value="<?= $viewdepartments['ID']; ?>"><?= $viewdepartments['persondepartment']; ?></option>
<?php
}
?>
</select>
</td>
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>
Is there anyway of retrieving whole tables based on a drop down list? Like when i select one and press submit it will bring that particular table? 'gameResults' is the database which features tables. My code is as follows:
if(isset($_POST["submit"])) {
$tablesnames = $_POSt["tablenames"]; // name of selection list
if($_POST["tablenames"] == '1') { // if option 1 is selected
// display table
} }
<?php
$conn = mysql_connect("localhost", "xxxxxx", "xxxxxx");
mysql_select_db("gameResults", $conn)
or die ('Database not found ' . mysql_error() );
"SELECT TABLE FROM information_schema.tables WHERE table_schema = 'gameResults'";
$rs = mysql_query($sql, $conn)
or die ('Problem with query' . mysql_error());
?>
<html>
<body>
<form name ="tables" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<p>Choose form to display :
<select name="tablesnames" id="tablesnames">
<option value="nothing"> </option>
<option value="fixtures"> Fixtures </option>
<option value="results"> Results </option>
<option value="teams"> Teams </option>
<option value="seasons"> Seasons </option>
<option value="administrators"> Administrators </option>
<option value="users"> Users </option>
</select></p>
<input type="submit" name="submit" > <input type="reset"
<html>
</body>
First off, there is no column TABLE. What you are looking for is TABLE_NAME. Consider this example: (And please stop using mysql_ functions, use PDO or mysqli_ instead)
<?php
$tables = array();
// host, db user, db password, datababse
$con = mysqli_connect("localhost","test","test","test");
$query = mysqli_query($con, "SELECT `TABLE_NAME` FROM `information_schema`.`tables` WHERE `TABLE_SCHEMA` = 'test'");
$tables = mysqli_fetch_all($query, MYSQLI_ASSOC);
?>
<!-- loop thru the values -->
<select name="whatever">
<?php foreach($tables as $value): ?>
<option value="<?php echo $value['TABLE_NAME']; ?>"><?php echo $value['TABLE_NAME']; ?></option>
<?php endforeach; ?>
</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.