Populating dropdown with sql results - php

I have the following code to display a dropdown based on the sql query but nothing is displaying when i run the code.
<?php
require ("common.php");
$sql = "SELECT FullName FROM Users";
$query = $db->prepare($sql);
$query->execute();
$option = "";
while($rows = $query->fetchAll(PDO::FETCH_ASSOC)) {
$name = $rows["FullName"];
$option.="<option>".$name."</option>";
}
?>
<div class="aClass">
<p class="select">Name</p>
<select name="aName" id="aName">
<option value="0">Select UserName</option>
<?php echo $option?>
</select>
</div>

$option you added an s
<?php echo $option;?>
Replace while with
foreach ($query->fetchAll(PDO::FETCH_ASSOC) as $rows) {
...
}
while($rows = $query->fetchAll(PDO::FETCH_ASSOC)) will return the entire set of results which will not evaluate to true,leaving $name undefined,you want to iterate over the results.

Related

Select an option in a dropdown box and display its associated data in php?

How to select a supplement and display its associated data such as its description and cost.
Any help will be appreciated.
PHP:
require('database.php');
$suppID = filter_input(INPUT_POST, 'suppID');
if($suppID==null || $suppID==false){
$suppID = 'Supplement-1';
}
$query = 'select * from tblsupplements where Supplement_id= :SupplementID';
$statement = $db->prepare($query);
$statement = bindValue(':SupplementID', $suppID);
$statement->execute();
$supplements = $statement->fetchAll();
$statement->closeCursor();
HTML:
<label>Supplement ID:</label>
<select name='suppID'>
<?php foreach ($supplements as $supplement): ?>
<option value="<?php echo $supplement['Supplement_id']; ?>">
<?php echo $supplement['Supplement_id']; ?>
</option>
<?php endforeach; ?>
</select>
Try this, hopefully helps.
<label>Supplement ID:</label>
<select name='suppID'>
<?php foreach ($supplements as $supplement) {
echo "<option value='".$supplement['Supplement_id']."'>".$supplement['Supplement_id']."</option>";
}
?>
</select>

How to get data from mysql to the select option in php

I have 2 tables: Students and Classes. In table Classes, I have a column "name" and in table Students I have a column "class" with input as select option. I want to get data from "name" to the select option. Anyone can help me?
This is my source code:
<?php
include "connect.php";
if (isset($_GET['id'])) {
$id = $_GET['id'];
$sqlClass = "select * from classes where id = $id";
$result = $conn->query($sqlClass);
if ($result->num_rows > 0) {
$classes[] = $result->fetch_assoc();
} else{
die('class not found');
}
}
$sql = "select count(name) from classes";
$total = $conn->query($sql);
$totalClass = $total->fetch_assoc()['count(name)'];
?>
<div class="form-group">
<label for="">Class</label>
<select name="" id="input" class="form-control" required="required">
<?php
for ($i=1; $i <= $totalClass ; $i++) {
?>
<option value="<?=$i?>"><?php echo $classes['name']; ?></option>
<?php } ?>
</select>
</div>
Your select needs a name (if you don't name it you will never find it when you post the data) and you don't need the second query to get a count, just an iterator for each option:
<?php
include "connect.php";
if (isset($_GET['id'])) {
$id = $_GET['id'];
$sqlClass = "select * from classes where id = $id";
$result = $conn->query($sqlClass);
if ($result->num_rows > 0) {
$classes[] = $result->fetch_assoc();
} else{
die('class not found');
}
}
?>
<div class="form-group">
<label for="">Class</label>
<select name="A_NAME_IS_REQUIRED" id="input" class="form-control" required="required">
<?php
$i = 1;
foreach($classes AS $class) {
?>
<option value="<?=$i?>"><?php echo $class['name']; ?</option>
<?php
$i++;
}
?>
</select>
</div>
In addition, you will want to check if $classes is an array. If it isn't there would be no sense in creating the drop-down.
SQL Injection Warning!
Little Bobby says your script is at risk for SQL Injection Attacks. Learn about prepared statements for MySQLi. Even escaping the string is not safe!

How to get php data into a html select?

I know that this question has been on multiple S.O. forums, but after reviewing, I seem to not be able to get the data elements in my phpmyadmin to appear in my html select. I am new to php, so therefore I am not very good at the fundamentals. Can anyone take a look at my code and tell me whats wrong with it? I am not getting any errors, just nothing in my html select.
Code:
<?php
$con = mysqli_connect('localhost','root','','Lab2_Database');
if($con-> connect_error) {
die("Connection Failed:".$con-> connect_error);
}
?>
<h1 id="header">Welcome to The Flight Club WebSite</h1>
<br>
<p>Select a Flight by Flight Number:</p>
<form>
<select>
<option value="0">Flight Number</option>
<?php
$sql = "SELECT flightNumber FROM Flight";
$result = $con-> query($sql);
while($row = mysql_fetch_assoc($get))
{
?>
<option value = "<?php echo($row['flightNumber'])?>" >
<?php echo($row['flightNumber']) ?>
</option>
<?php
}
?>
</select>
</form>
</body>
</html>
$result = $con-> query($sql);
while($row = mysql_fetch_assoc($get))
{
?>
<option value = "<?php echo($row['flightNumber'])?>" >
<?php echo($row['flightNumber']) ?>
</option>
<?php
}
change with
$result = $con-> query($sql);
while($row = mysql_fetch_assoc($result))
{
?>
<option value = "<?php echo($row['flightNumber'])?>" >
<?php echo($row['flightNumber']) ?>
</option>
<?php
}
variable $get is not defined i think
you are using mysql with mysqli object
$result = $con-> query($sql);
while($row = mysql_fetch_assoc($get))
{
?>
change this to
$result = $con-> query($sql);
while($row = $result->fetch_assoc())
{
?>
http://php.net/manual/en/mysqli-result.fetch-assoc.php

Inserting selected value from populated mysql dropdown

I am trying to insert the selected value from a drop down that was populated from a reference table in my database. I followed a tutorial for a dynamic dropdown but now I would like to take the value and insert it. The problem is it keeps taking the echo the tutorial uses. Is there a way I can make that selected value a new variable? It currently inserts "< php echo $team_name"
<div>
<label>Home Team</label>
<select name="home_team" style="width:125px;>
<option value="">Select Team</option>
<?php
$query = "SELECT * FROM team";
$results = mysqli_query($db, $query);
mysqli_query($db, "SELECT * FROM team_name");
// loop
foreach ($results as $team_name) {
?>
<option value="<php echo $team_name["cid"]; ?><?php echo $team_name["team_name"]; ?></option>
<?php
}
?>
</select>
How I attempted to insert:
$db = mysqli_connect('localhost', 'root', 'root', 'register');
if(mysqli_connect_errno())
{
echo "failed" . mysqli_connect_error();
}
//var_dump($_POST);
$home_team = mysqli_real_escape_string($db, $_POST['home_team']);
$home_team = $home_team;
$query = "INSERT INTO game_table (home_team)
VALUES('$home_team')";
mysqli_query($db, $query);
//echo $query;
//echo $home_team;
//header('location: index.php');
please follow this.
<select name="home_team" style="width:125px;>
<option value="">Select Team</option>
<?php
$query = "SELECT * FROM team";
$results = mysqli_query($db, $query);
while($row = mysqli_fetch_assoc($results)) {
?>
<option value="<php echo $row['cid']; ?>"><?php echo $row["team_name"]; ?></option>
<?php } ?>
</select>
may be this should work
Try this. There was a couple of missing " and ? in your code.
<select name="home_team" style="width:125px;">
<option value="">Select Team</option>
<?php
$query = "SELECT * FROM team";
$results = mysqli_query($db, $query);
foreach ($row = mysqli_fetch_assoc($results)) {
?>
<option value="<?php echo $row["cid"]; ?>">
<?php echo $row["team_name"]; ?>
</option>
<?php
}
?>
</select>

Displaying a list of checkboxes based on an array

I have a list of checkboxes displayed below. This shows all the contactors and allows them to be selected via check box.
<?php
$query = "SELECT * FROM form_4 GROUP BY contractors ASC";
$result = mysql_query($query);
?>
<li><select multiple="multiple" size="10" name="contractors[]">
<option value="None Yet" selected="selected">None Yet
</option>
<?php
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
?>
<option value="<?php echo $line['contractors'];?>"> <?php echo $line['contractors'];?> </option>
<?php
}
?>
</select></li>
I have an array saved in another place that I would like to generate the list above but with the items in the array below already checked/selected.
<?php
$options = unserialize('contractors');
$result = mysql_query("SELECT * FROM form_2 WHERE jobname = 'testjob' GROUP BY jobname ORDER BY biddate ASC LIMIT 0, 1");
while($row = mysql_fetch_array($result))
{
$contractors = unserialize($row['contractors']);
foreach ($contractors as $contractor)
echo "" . htmlspecialchars ($contractor).' - ';
?>
Any help would be greatly appreciated.
Try this :
<option value="<?php echo $line['contractors'];?>" <?php if(in_array($line['contractors'],$contractors)){?>checked="checked" <?php }?>> <?php echo $line['contractors'];?> </option>

Categories