Simple Dropdown List PHP code from MYSQL - php

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

Related

Populate drop down list from mysql in in php html

I am using the below code to populate drop down list in php html,
<?php
$mid="mario";
$sql = "SELECT * FROM tbl_prdy WHERE col_master_id = '$mid'";
$result = mysqli_query($conn,$sql);
echo "<select name='list'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['col_of_fa'] . "'>" . $row['col_of_fa'] . "
</option>";
}
echo "</select>";
?>
But, I am getting internal server error. I have debugged the code and found that the issue is with the following 2 lines in the above code. There is not much information in server logs. Can you tell me what might be the issue with the following 2 lines of code?
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['col_of_fa'] . "'>" . $row['col_of_fa'] .
"/option>";
}
mixing mysqli with mysql
change
$row = mysql_fetch_array($result)
to
$row = mysqli_fetch_array($result)
You would use this
while($row=mysqli_fetch_assoc($result )){
}
or
while($row=mysqli_fetch_array($result )){
}
//Try this :
while ($row = mysqli_fetch_array($result)) { ?>
<option value="<?php echo $row['col_of_fa'] ?>" ><?php echo $row['col_of_fa'] ?>
</option>
<?php }

Populating Php Dropdown list from mysql database [duplicate]

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>

how to get value from database and show in dropdown list

I already try many ways but the value didn't show in dropdown list
Here, this is my code. can you suggest me anything that i was wrong
<?php
$result = mysqli_query($con,"SELECT * FROM project");
if( mysqli_num_rows( $result )==0){
echo "<tr><td>No Rows Returned</td></tr>";
}else{
$row = mysqli_fetch_assoc( $result );
$pos = 0;
echo "<select name=Pname >";
while($pos <= count ($row)){
echo "<option value="$row["project_no"]">"$row["project_name"]"</option>";
$pos++;
}
echo "</select>";?>
And i write as .php file. Thanks for your help.
Try this out:
$output = '';
if(mysqli_num_rows($result) == 0){
// echo error;
} else {
while($row = mysqli_fetch_assoc($result)){
$project_no = $row['project_no'];
$project_name = $row['project_name'];
$output .= '<option value="' . $project_no . '">' . $project_name . '</option>";
}
}
Then inside of your HTML, print your $output variable inside of your <select> element:
<select>
<?php
print("$output");
?>
</select>
It should print all options for every row that you have requested from the database.
Hope this helps :)
Try this:
$result = mysqli_query($con,"SELECT * FROM project");
if( mysqli_num_rows( $result )==0){
echo "<tr><td>No Rows Returned</td></tr>";
}else{
echo "<select name=Pname >";
while ($row = mysqli_fetch_assoc($result)) {
echo "<option value="$row["project_no"]">"$row["project_name"]"</option>";
}
echo "</select>";
}
This is the result code that i can run it. I put this code in a form code of html
$result = mysqli_query($con,"SELECT * FROM project"); ?>
<?php
$output = '';
if(mysqli_num_rows($result) == 0){
// echo error;
} else {
echo " <select name = Pname>";
while($row = mysqli_fetch_assoc($result)){
$project_no = $row['project_no'];
$project_name = $row['project_name'];
$output = "<option value=" . $project_no . "> ". $project_name ." </option>";
print("$output");
}
echo " </select>";
}
?>
Thank you every one for helping me ^^

Trouble storing value of drop down, and using in query

Hi I am having trouble getting this to store the value when submitted and then using it in my next query. The drop down is populated fine, but it is either not storing the value or the way I am using it dynamically in my query is wrong. Any help is appreciated. I know I am probably missing something obvious or small, I am new to php.
drop down box
$sth = $db->prepare("SELECT DISTINCT WEEK FROM Player_Points_2013");
//$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
$sth->execute();
echo '<form method="POST" action=" " >';
echo '<select id="week" name="week"><OPTION>';
echo "Select a Week to see the stats</OPTION>";
while ($row = $sth->fetch(PDO::FETCH_ASSOC))
{
echo "<option value=\"$WEEK\">" . $row['WEEK'] . "</option>";
}
echo '</SELECT>';
echo '<input type="submit" name="table_stats" value="Submit"/>';
echo '</form>';
$select_val = $_POST['week'];
//use value from drop down to display weeks stats
if(isset($_POST['table_stats'])){
$sql = $db->prepare("SELECT PName,CombinedPoints FROM `Player_Points_2013` WHERE
WEEK = '$select_val' ORDER BY CombinedPoints ");
//$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
$sql->execute();
Print "<table border cellpadding=10 width=500>";
Print "<tr><th>Player</th><th>Points</th></tr>";
while ($row = $sql->fetch(PDO::FETCH_ASSOC))
{
Print " <td>".$row['PName'] . "</td> ";
Print " <td>".$row['CombinedPoints'] . "</td> </tr>";
}
Print "</table>";
}
else {
echo $select_val;
}
?>
Use either $row or $info but not both, e.g.:
while ($row = $sql->fetch(PDO::FETCH_ASSOC))
{
Print " <td>".$row['PName'] . "</td> ";
Print " <td>".$row['CombinedPoints'] . "</td> </tr>";
}
Also you have code:
echo "<option value=\"$WEEK\">" . $row['WEEK'] . "</option>";
What is $WEEK? Maybe it should be $row['WEEK'] instead?

Updating to MySQLi and having some trouble

So I've spent a bit of time looking at MySQLi and I'm having trouble updating a script with the new functions. This script is used for a dynamic dropdown form, using data sent to it using JS. You can find a live version of the script here to check out what I'm takling about. I've looked up and down my code and have compared it to other MySQLi examples and I'm just not sure where I'm going wrong.
Now, the first dropdown doesn't even initiate a query, all the PHP does is return predefined results as it's just simpler for the first option. What's weird, to me, is that even the first drop down is now not working when it does not rely at all on the MySQLi connection. It all worked before updating, just for reference.
Here's my script:
$db = new mysqli($dbHost, $dbUser, $dbPass, $dbDatabase);
if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}
//prevents injections
//any order
isset($_GET['type'])?$type = urldecode($_GET['type']):"";
isset($_GET['source'])?$source = $db->real_escape_string(urldecode($_GET['source'])):"";
isset($_GET['range'])?$power = $db->real_escape_string(urldecode($_GET['range'])):"";
isset($_GET['setpoint'])?$setpoint = $db->real_escape_string(urldecode($_GET['setpoint'])):"";
//forms the query depending on what data is recieved through GET
//first option on the bottom; last option on the top to avoid conflicts
if (isset($_GET['setpoint'])) {
$query = "SELECT DISTINCT stp FROM meters WHERE sio='$range' AND pso='$power' AND stp='$setpoint' ORDER BY model";
} elseif (isset($_GET['power'])) {
$query = "SELECT DISTINCT stp FROM meters WHERE sio='$range' AND pso='$power' ORDER BY model";
} elseif (isset($_GET['range'])) {
$query = "SELECT DISTINCT pso FROM meters WHERE sio='$range' ORDER BY model";
} elseif (isset($_GET['source'])) {
$query = "SELECT DISTINCT sir FROM meters WHERE sio LIKE '%$source%' ORDER BY sir";
}
//creates a result array from query results
isset($query)?$result = $db->query($query):"";
//outputs dropdown options dependent on what GET variables are set
//first option on the bottom; last option on the top to avoid conflicts
if (isset($_GET['setpoint'])) {
while ($row = $result->fetch_assoc()) {
echo "<option value='" . $row['stp'] . "'>" . $row['stp'] . "</option>";
$result->free();
}
} elseif (isset($_GET['power'])) {
echo "<option>Please Choose Setpoint Options</option>";
while ($row = $result->fetch_assoc()) {
$row{'stp'} = ucfirst($row{'stp'}); //capitalizes the first letter; necessary?
echo "<option value='" . $row['stp'] . "'>" . $row['stp'] . "</option>";
$result->free();
}
} elseif (isset($_GET['source'])) {
echo "<option>Please Choose Input Range</option>";
while ($row = $result->fetch_assoc()) {
echo "<option value='" . $row['sir'] . "'>" . $row['sir'] . "</option>";
$result->free();
}
} elseif (isset($_GET['type']) && $_GET['type'] == "Digital") {
echo "<option>Please Choose Input Source</option>";
echo "<option value='RS232C'>RS232C</option><option value='RS422'>RS422</option><option value='RS485'>RS485</option><option value='current loop'>current loop</option>";
$result->free();
} elseif (isset($_GET['type']) && $_GET['type'] == "Analog") {
echo "<option>Please Choose Input Source</option>";
echo "<option value='DC current'>DC Current</option><option value='DC voltage'>DC Voltage</option><option value='AC current'>AC Current</option><option value='AC voltage'>AC Voltage</option><option value='process'>Process</option><option value='thermocouple'>Thermocouple</option><option value='RDT'>rdt</option>";
$result->free();
}
edit: This is my old code using the deprecated method.
$con = mysql_connect($dbHost, $dbUser, $dbPass) or trigger_error("Failed to connect to MySQL Server. Error: " . mysql_error());
mysql_select_db($dbDatabase) or trigger_error("Failed to connect to database {$dbDatabase}. Error: " . mysql_error());
//prevents injections
//any order
isset($_GET['type'])?$type = urldecode($_GET['type']):"";
//$type = mysql_real_escape_string(urldecode($_GET['type']));
isset($_GET['source'])?$source = mysql_real_escape_string(urldecode($_GET['source'])):"";
isset($_GET['range'])?$power = mysql_real_escape_string(urldecode($_GET['range'])):"";
isset($_GET['setpoint'])?$setpoint = mysql_real_escape_string(urldecode($_GET['setpoint'])):"";
//forms the query depending on what data is recieved through GET
//first option on the bottom; last option on the top to avoid conflicts
if (isset($_GET['setpoint'])) {
$query = "SELECT DISTINCT stp FROM meters WHERE sio='$range' AND pso='$power' AND stp='$setpoint' ORDER BY model";
} elseif (isset($_GET['power'])) {
$query = "SELECT DISTINCT stp FROM meters WHERE sio='$range' AND pso='$power' ORDER BY model";
} elseif (isset($_GET['range'])) {
$query = "SELECT DISTINCT pso FROM meters WHERE sio='$range' ORDER BY model";
} elseif (isset($_GET['source'])) {
$query = "SELECT DISTINCT sir FROM meters WHERE sio LIKE '%$source%' ORDER BY sir";
}
//creates a result array from query results
isset($query)?$result = mysql_query($query):"";
//outputs dropdown options dependent on what GET variables are set
//first option on the bottom; last option on the top to avoid conflicts
if (isset($_GET['setpoint'])) {
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row{'stp'} . "'>" . $row{'stp'} . "</option>";
}
} elseif (isset($_GET['power'])) {
echo "<option>Please Choose Setpoint Options</option>";
while ($row = mysql_fetch_array($result)) {
$row{'stp'} = ucfirst($row{'stp'}); //capitalizes the first letter; necessary?
echo "<option value='" . $row{'stp'} . "'>" . $row{'stp'} . "</option>";
}
} elseif (isset($_GET['source'])) {
echo "<option>Please Choose Input Range</option>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row{'sir'} . "'>" . $row{'sir'} . "</option>";
}
} elseif (isset($_GET['type']) && $_GET['type'] == "Digital") {
echo "<option>Please Choose Input Source</option>";
echo "<option value='RS232C'>RS232C</option><option value='RS422'>RS422</option><option value='RS485'>RS485</option><option value='current loop'>current loop</option>";
} elseif (isset($_GET['type']) && $_GET['type'] == "Analog") {
echo "<option>Please Choose Input Source</option>";
echo "<option value='DC current'>DC Current</option><option value='DC voltage'>DC Voltage</option><option value='AC current'>AC Current</option><option value='AC voltage'>AC Voltage</option><option value='process'>Process</option><option value='thermocouple'>Thermocouple</option><option value='RDT'>rdt</option>";
}
You are freeing your $result inside the while loop. This will cause the loop to fail on the second iteration.
Since you are freeing the result in all of the ifs, why don't you just do it once at the end?
...
} elseif (isset($_GET['type']) && $_GET['type'] == "Analog") {
echo "<option>Please Choose Input Source</option>";
echo "<option value='DC current'>DC Current</option><option value='DC voltage'>DC Voltage</option><option value='AC current'>AC Current</option><option value='AC voltage'>AC Voltage</option><option value='process'>Process</option><option value='thermocouple'>Thermocouple</option><option value='RDT'>rdt</option>";
}
$result->free();
However this doesn't explain why analog and digital won't work still..

Categories