Want to show data in combo box from mysql table - php

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.

Related

How to write WHERE referring to a drop down list select?

There is a drop-down list on the PHP website that contains names taken from the database,
after selecting a name from the list, e.g. "Aprilia", I would like all records to be displayed from the database
where mark = Aprilia
I know I should add in the code below just in a SELECT WHERE x = y query
but I just don't know how to do it;
it should look like this in my opinion:
$result = mysqli_query($conn, "SELECT * FROM motorcycles WHERE mark = X");
And I just can't find this X (X should be the user pick from the dropdown list)
How to write it?
Photos:
https://imgur.com/a/PMu4At7
<?php
require_once 'header.php';
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "projectinz";
// Create connection
//$conn = new mysqli($servername, $username, $password, $dbname);
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
<select name="mark" id="mark">
<?php
$query = $conn->query("SELECT mark FROM motocykle");
while($kategoria = mysqli_fetch_array($query))
{
echo '<option>'.$kategoria['mark'].'</option>';
}
?>
</select>
<?php
$wynik = mysqli_query($conn,"SELECT * FROM motocykle");
while($row = mysqli_fetch_array($wynik))
{
echo "<br>".$row['mark']." ".$row['model']." ".$row['capacity']." ".$row['power']."<br>";
}
mysqli_close($conn);
?>
</body>
</html>
EDIT !!!
find1.php
<?php
require_once 'header.php';
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "projectinz";
// Create connection
//$conn = new mysqli($servername, $username, $password, $dbname);
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
<form action="../includes/find.inc.php" method="post">
<select name="mark" id="mark">
<?php
$query = $conn->query("SELECT mark FROM motocykle");
while ($kategoria = mysqli_fetch_array($query)) {
echo '<option value="id">'.$kategoria['mark'].'</option>';
}
?>
</select>
<button type="submit" name="findmoto">Find</button>
</form>
<?php
$wynik = mysqli_query($conn,"SELECT * FROM motocykle ");
while ($row = mysqli_fetch_array($wynik)) {
echo "<br>".$row['mark']." ".$row['model']." ".$row['capacity']." ".$row['power']."<br>";
}
mysqli_close($conn);
?>
</body>
</html>
find.inc.php
<?php
session_start();
if (isset($_POST['findmoto'])) {
require 'dbh.inc.php';
$id = $_POST["id"];
$marka = $_POST["mark"];
echo $id;
echo $marka;
$display = "SELECT * FROM motocykle WHERE id='$id';";
$run = mysqli_query($conn, $display);
if ($run) {
echo $display;
} else {
echo "not";
}
}
BUT:
https://imgur.com/a/RA5wuus
Where is the problem?
<option value="1">Name</option> has attribute value witch is sent to POST when form is submitted. You need to set it with probably ID from table and then after POST you filter by that ID in WHERE part of your SQL.
First change
<select name="mark" id="mark">
<?php
$query = $conn->query("SELECT id, mark FROM motocykle");
while($kategoria = mysqli_fetch_array($query))
{
echo '<option value="'.$kategoria['id'].'">'.$kategoria['mark'].'</option>';
}
?>
</select>
Second change
if(isset($_POST['findmoto'])) {
require 'dbh.inc.php';
$selectedId = intval($_POST["mark"]);
echo $selectedId;
$display = "SELECT * FROM motocykle WHERE id='$selectedId';";
$run = mysqli_query($conn, $display);
if($run)
{
echo $display;
}
else
{
echo "not";
}
}

echo something out MySQL database

I am trying to get a question with answers out of my database. I just want to get one thing out of the database and not with a row. I thought this would work but it puts out this: Resource id #4 can someone explains what I am missing.
Thanks :)
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db('lotto');
$sql = 'SELECT id, vraag, AntwA, AntwB, AntwC, AntwD FROM vraag1';
$test = mysql_query($sql);
echo $test;
?>
As said at least 10000 times everywhere in internet, never use MySQL_ ! (If your are trying to learn something new by using tutorials over internet, don't use old ones)
I recommend to use PDO which is modern API in PHP and a lot more secure when using it correctly with prepared statement ! But you can also use MYSQLI which is more similar to the MYSQL !
You have to export your data from return array :
Using PDO :
$db = new PDO ("mysql:host=".$hostname.";dbname=".$dbname, $username, $password);
$query = $db -> prepare ("SELECT * FROM vraag1");
$query -> execute (array ());
$rows = $query -> fetchAll (PDO::FETCH_ASSOC);
foreach ($rows as $row)
{
echo $id = $row["id"];
echo $vraag = $row["vraag "];
echo $AntwA = $row["AntwA "];
echo $AntwB = $row["AntwB "];
echo $AntwC = $row["AntwC "];
echo $AntwD = $row["AntwD "];
}
Using MYSQLI :
$db = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
$query = "SELECT * FROM vraag1";
$rows = mysqli_query($db, $query);
while($row = mysqli_fetch_assoc($rows))
{
echo $row["id"];
echo $row["vraag"];
echo $row["AntwA"];
echo $row["AntwB"];
echo $row["AntwC"];
echo $row["AntwD"];
}
First of all the mysql function you are using is depreciated and no longer supported. you should use mysqli or pdo instead with prepared statements.
<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "lotto";
// Create connection
$conn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT id, vraag, AntwA, AntwB, AntwC, AntwD FROM vraag1";
$test = mysqli_query($conn, $sql);
if (mysqli_num_rows($test) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($test)) {
echo "ID : ".$row['id']."<br>";
echo "vraag :".$row['vraag']."<br>";
echo "AntwA :".$row['AntwA']."<br>";
echo "AntwB :".$row['AntwB']."<br>";
echo "AntwC :".$row['AntwC']."<br>";
echo "AntwD :".$row['AntwD']."<br>";
}
} else {
echo "no results found";
}
mysqli_close($conn);
?>
For select function mysql_query() returns a resource on success, or FALSE on error.
so your assignment statement
$test = mysql_query($sql);
assign the resource to $test.
if you want the data inside the resource you can do
while($row= mysql_fetch_assoc($test)):
print_r($row);
endwhile;
also you can access the $row['column_name']
If you want to return only one row you can do this limit in query
$sql = 'SELECT id, vraag, AntwA, AntwB, AntwC, AntwD FROM vraag1 limit 1';
You need to add something like the following:
while($row = mysql_fetch_array($result)){
echo $row['id'];
echo $row['vraag'];
echo $row['AntwA'];
echo $row['AntwB'];
echo $row['AntwC'];
echo $row['AntwD'];
}
use mysqli instead of mysql
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
mysqli_select_db('lotto');
$sql = 'SELECT id, vraag, AntwA, AntwB, AntwC, AntwD FROM vraag1';
$test = mysqli_query($sql);
echo $test;
?>

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>";
?>

dropdown to show table in DB

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
?>

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