dropdown to show table in DB - php

I am working on the following code where the user chooses a table from a dropdown. On change, it displays the table but at the moment it is echoing some of the code to screen.
<?php
$dbh = "localhost";
$dbn = "dbname";
$dbu = "dbuser";
$dbp = "dbpassword";
$conn = mysql_connect($dbh,$dbu,$dbp) or die("Unable to connect do database.");
mysql_select_db($dbn, $conn) or die("Unable to select database.");
$result = mysql_query("SHOW TABLES FROM $dbn") or die("Cannot list table names.");
echo "
<form name=\"table_browser\" action=\"".$PHP_SELF."\" method=\"GET\" >
<select name=\"t\" onChange=\"javascript:submit();\">
<option>Select a table</option>
";
while ($row = mysql_fetch_row($result)){
echo " <option value=".$row[0].">".$row[0]."</option>\n";
}
echo " </select>
</form>\n";
if (!isset($t)){
die("Please select a table");
}
?>

You need to use full php code here
<?php
?>

Related

Data Not Displaying in Drop Down by mysqli

I have a small data entry form which works well, but someone asked me if I can have the 'name' field as a drop down box of users as opposed to having to type in a name and risk a spelling mistake - yep makes sense.
This is pretty new to me and following some information on here and other sites I have tried to accomplish the first part.. populating the drop down box.. nope. No errors, just nothing in the box.
To power this I have tblStaffNames (userID, txtName)
The code I am using looks like this;
<?php
include("connect-db.php");
$queryNames = "SELECT txtName FROM tblStaffName";
$resultNames = $conn->query($queryNames);
?>
<select name="personname">
<?php
while ($rowNames = $resultNames->fetch_assoc()) {
echo "<option value=\"{$rowNames['txtName']}\">";
echo $rowNames['txtName'];
echo "</option>";
}
?>
</select>
The $conn is all good as on another page I can display data in a table from the database, including tblStaffNames - so I can rule out any sort of connection issues.
When I run the page, the little drop down box appears, very simple like but it's there, just no values.
I will end up using the value like this as part of the data entry form;
<td><select name="personname" style="width:100px" ><?php echo $RowNames; ?></select></td>
But I can't actually get to the point of displaying data.
Can anyone help me out with what I am doing wrong here?
I tested your code with slight changes. It works for me. Please check your DB connection is OK as I have done in my code.
<?php
$servername = "localhost";
$username = "user";
$password = "password";
$dbname = "staff_db";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$queryNames = "SELECT txtName FROM tblStaffName";
$resultNames = $conn->query($queryNames);
//Generating the Dropdown
echo "<select name=\"personname\">";
while ($rowNames = $resultNames->fetch_assoc()) {
echo "<option value=\"{$rowNames['txtName']}\">";
echo $rowNames['txtName'];
echo "</option>";
}
echo "</select>";
?>
I dont know what your error but for better understanding i change your code like this
$queryNames = "SELECT userID,txtName FROM tblStaffName";
$resultNames = $conn->query($queryNames);
?>
<select name="personname">
<?php
while ($rowNames = $resultNames->fetch_assoc()) {
?>
<option value="<?php echo $rowNames['userID']; ?>">
<?php echo $rowNames['txtName']; ?>
</option>
<?php
}
?>
</select>
Following code snippets works for me.
<?php
$servername = "localhost";
$username = "user";
$password = "password";
//DB Server Connection
$conn = mysql_connect($servername, $username, $password) or die("Connection establishment failed");
//DB Selection
$selected = mysql_select_db("staff_db", $conn) or die("Could not select DB");
//Query String
$queryNames = "SELECT txtName FROM tblStaffName";
//Query the DB
$resultNames = mysql_query($queryNames);
//Generating the Dropdown
echo "<select name=\"personname\">";
while ($rowNames = mysql_fetch_array($resultNames)) {
echo "<option value=\"{$rowNames['txtName']}\">";
echo $rowNames['txtName'];
echo "</option>";
}
echo "</select>";
?>

populating dropdown list from mysql - php/html

Trying to populate a dropdown list from my database - connection is ok and in my mind the code I have should work, but currently getting a blank dropdown...
Have looked at PHP- Fetch from database and store in drop down menu html as well as other tutorials but alas no luck so far!
code is as follows...
<?php
//get constants for database
require("database.php");
// Opens a connection to a MySQL server
$connection = mysqli_connect ($server, $username, $password);
if (!$connection){
die('Not connected : ' . mysqli_error());
}
// Set the active MySQL database
$db_selected = mysqli_select_db($connection, $database);
if (!$db_selected) {
die ('Can\'t use db : ' . mysqli_error($connection));
}
$query = "SELECT * FROM route WHERE 1";
$result = mysqli_query($connection, $query);
echo '<select name="list" style="width:400px;">';
while($r = mysqli_fetch_assoc($result)){
echo "<option value=".$r['alt']."</option>";
}
echo '</select>';
?>
<option> tag is broken.
Corrected code:
while($r = mysqli_fetch_assoc($result)){
echo '<option value="'.$r['alt'].'">'.$r['alt'].'</option>';
}
Note: You can use single and double quotes either, but, they should be properly closed.
Please have a look on this Select Dropdown Syntax
Syntax For Select Dropdown
<select>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
</select>
You have missed a closing of value in option.
<?php
//get constants for database
require("database.php");
// Opens a connection to a MySQL server
$connection = mysqli_connect ($server, $username, $password);
if (!$connection){
die('Not connected : ' . mysqli_error());
}
// Set the active MySQL database
$db_selected = mysqli_select_db($connection, $database);
if (!$db_selected) {
die ('Can\'t use db : ' . mysqli_error($connection));
}
$query = "SELECT * FROM route WHERE 1";
$result = mysqli_query($connection, $query);
//Changes From Here
?>
<select name="list" style="width:400px;">
<?
while($r = mysqli_fetch_assoc($result))
{?>
<option value="<?echo $r['alt'];?>"><?echo $r['alt'];?></option>
<?}?>
</select>
<?php
//get constants for database
require("database.php");
// Opens a connection to a MySQL server
$connection = mysqli_connect ($server, $username, $password);
if (!$connection){
die('Not connected : ' . mysqli_error());
}
// Set the active MySQL database
$db_selected = mysqli_select_db($connection, $database);
if (!$db_selected) {
die ('Can\'t use db : ' . mysqli_error($connection));
}
$query = "SELECT * FROM route WHERE 1";
$result = mysqli_query($connection, $query);
echo '<select name="list" style="width:400px;">';
while($r = mysqli_fetch_assoc($result)){
echo "<option value=".$r['alt'].">".$r['alt']."</option>";
}
echo '</select>';
?>
so if your select condition is right/hidden by you then you will have to add a display value between option tag. As per the code you have assigned values in option value tag but not given the values to be displayed. Try the above

Want to show data in combo box from mysql table

I want to show a combo box data from mysql table. Below is the code.
But it is not working.i am unable to find the error. can anyone please help me.
<select name="priority">
<?php
$server = 'localhost';
$user = 'root';
$pass = '1amShaw0n';
$db = 'shawon_logindb';
$db = new mysqli($server, $user, $pass, $db) or die("Unable to connect");
$query = "SELECT * FROM members";
$result = mysql_query($query);
while($row=mysql_fetch_assoc($result)){
?>
<option value="<?php echo isset($row["username"])?$row["username"]:''; ?>"> <?php echo isset($row["username"])?$row["username"]:''; ?></option>
<?php } ?>
</select>
Please help me to get the solution.
Conflicting mysql and mysqli.
There is two values in $db variable.
Remove isset inside <option>, because its only execute when its having data.
$conn = new mysqli($server, $user, $pass, $db) or die("Unable to connect");
$query = "SELECT * FROM members";
$result = mysqli_query($conn, $query);
while($row=mysqli_fetch_array($result, MYSQLI_ASSOC))
{
?>
<option value="<?php echo $row['username']; ?>"> <?php echo $row["username"]; ?></option>
<?php
}
?>
Use below code:
$db = new mysqli($server, $user, $pass, $db) or die("Unable to connect");
$query = "SELECT * FROM members";
$result = $db->query($query);
while($row=$result->fetch_assoc()){
?>
<option value="<?php echo isset($row['username']) ? $row['username'] : ''; ?>"><?php echo isset($row["username"]) ? $row["username"] : ''; ?></option>
<?php } ?>
Don't mix the mysql & mysqli functions.

How to load php file to dynamically load dropdown

I have a PHP script that dynamically shows the 'course' options from my database
<?php
$db_host = 'localhost';
$db_user = 'root';
$db_pass = '';
$db_name = '';
$con = mysqli_connect($db_host,$db_user,$db_pass, $db_name);
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
$sql = "SELECT courseID, name FROM courses";
$result = mysqli_query($con, $sql) or die("Error: ".mysqli_error($con));
while ($row = mysqli_fetch_array($result))
{
echo'<option value="'.$row['courseID'].'">'.$row['name'].'</option>';
}
?>
I have a dropdown in my HTML (scorecard.php) page.
<form> <select id="selectCourse" > <option value = "">Select Course</option></select></form>
I was wondering would anyone know a script or way of getting this data to display in my dropdown.
Thanks for any help
You have to run your while loop inside the select tag
Here is updated code
<?php
$db_host = 'localhost';
$db_user = 'root';
$db_pass = '';
$db_name = '';
$con = mysqli_connect($db_host,$db_user,$db_pass, $db_name);
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
$sql = "SELECT courseID, name FROM courses";
$result = mysqli_query($con, $sql) or die("Error: ".mysqli_error($con));
?>
<form>
<select id="selectCourse" >
<option value = "">Select Course</option>
<?php while ($row = mysqli_fetch_array($result))
{
echo'<option value="'.$row['courseID'].'">'.$row['name'].'</option>';
} ?>
</select>
echo'<option value="'.$row['courseID'].'">'.$row['name'].'</option>';
change it to
$courses[] = '<option value="'.$row['courseID'].'">'.$row['name'].'</option>';
and where u want it to be displayed
foreach($courses as $c){
echo $c;
}

php query result in html option

Why do I get a white row and not the value?
<select name="select">
<?php
$connessione = mysql_connect('localhost' , 'root', '') or die("Impossibile connettersi: " .mysql_error());
mysql_select_db("musica",$connessione);
$query = mysql_query("SELECT * FROM artisti_preferiti");
while($row = mysql_fetch_array($query))
{
?><option value="<?php echo $row['nome']; ?>"> <?php echo $row['cognome'];?></option>
<?php }?>
</select>
I suspect this being a DB connection issue, where I successfully tested this.
Consider the following:
Sidenote: Make sure that your settings are correct, including the DB name, and colum names.
DB connection file: (db_connect.php)
<?php
$mysql_hostname = 'xxx';
$mysql_username = 'xxx';
$mysql_password = 'xxx';
$mysqli = mysql_connect("$mysql_hostname", "$mysql_username", "$mysql_password");
if($mysqli->connect_errno > 0) {
die('Connection failed [' . $mysqli->connect_error . ']');
}
?>
PHP (example.php)
<select name="select">
<?php
include 'db_connect.php';
mysql_select_db("musica",$mysqli);
$query = mysql_query("SELECT * FROM artisti_preferiti",$mysqli);
while($row = mysql_fetch_array($query))
{
?><option value="<?php echo $row['nome']; ?>"> <?php echo $row['cognome'];?></option>
<?php }?>
</select>
On a final note, mysql_* functions are deprecated. Do consider using mysqli_* with prepared statements or PDO.
EDIT
MySQLi_* version
Sidenote: It is best using column names instead of SELECT * --- i.e.: SELECT nome, cognome
DB connection file: (db_connect_mysqli.php)
<?php
$mysql_hostname = 'xxx';
$mysql_username = 'xxx';
$mysql_password = 'xxx';
$mysql_dbname = 'xxx';
$mysqli = new mysqli("$mysql_hostname", "$mysql_username", "$mysql_password","$mysql_dbname");
if($mysqli->connect_errno > 0) {
die('Connection failed [' . $mysqli->connect_error . ']');
}
?>
PHP (example_mysqli.php)
<select name="select">
<?php
include 'db_connect_mysqli.php';
$query = $mysqli->query("SELECT * FROM artisti_preferiti");
while($row = mysqli_fetch_array($query))
{
?><option value="<?php echo $row['nome']; ?>"> <?php echo $row['cognome'];?></option>
<?php }?>
</select>
I changed your script so that we can debug
<?php
$lnk = mysql_connect('localhost' , 'root', '') or die("Impossibile connettersi: " .mysql_error());
mysql_select_db("musica",$lnk);
$q = 'SELECT nome,cognome FROM artisti_preferiti';
$result = mysql_query($q,$lnk);
$numRows = mysql_num_rows($result);
$rows = array();
while($row = mysql_fetch_array($result)) $rows[$row['nome']] = $row['cognome'];
/* show us this!! */
print_r($rows);
?>
<select name="select"><?php
foreach ($rows as $nome => $cognome) {
echo "<option value='$nome'>$cognome</option>";
}
?></select>

Categories