How to get php data into a html select? - php

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

Related

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>

Get value of mysql database in select menu

I have issue with select menu on PHP. I tried to get mysql database to select menu. However, it displays none.
Here is my code:
default:
mysql_select_db($database_conn, $conn);
$query_Rsenroll = "SELECT * FROM `tbl_enroll` WHERE `tbl_enroll`.`courseid` ='".$_GET['courseid']."'";
$Rsenroll = mysql_query($query_Rsenroll, $conn) or die(mysql_error());
$row_Rsenroll = mysql_fetch_assoc($Rsenroll);
$totalRows_Rsenroll = mysql_num_rows($Rsenroll);
$courseid = $row_Rsenroll['courseid'];
$er_staffid = "";
break;
}
?>
<select name="courseid">
<option value="" SELECTED>Selected Course ID</option>
<?php
foreach( $Course as $course_id) {
if ( $course_id == $courseid) {
$selected = " SELECTED";
} else {
$selected = "";
}
?>
<option value="<?php echo $course_id; ?>"<?php echo $selected; ?>><?php echo $row_Rsenroll['courseid']; ?></option>
<?php
}
?>
</select>
Thank you for any help and advice.
Assuming courseid is being passed as a variable in the sending URL (file.php?courseid=COURSEID), I think this should do what you want:
This might clean up your script a little (though I switched it to mysql_fetch_array as I'm more familiar with that than mysql_fetch_assoc. Feel free to use assoc):
<?php
$cid = '6116';
?>
<select name="courseidMenu">
<option value="" SELECTED>Selected Course ID</option>
<?php
$query = mysql_query("SELECT * FROM tbl_enroll WHERE courseid = '$cid'", $conn)or die(mysql_error());
$total_rows = mysql_num_rows($query);
while($row = mysql_fetch_array($query)){
$courseId = $row['courseid'];
?>
<option value="<?=$courseId?>" ><?=$courseId?></option>
<?
}
?>
</select>
updated use this it is working on my portal <select>
<option value=''>Select Provider</option>
<?php
$server="server name";
$user="user name";
$password="password";
$database="database";
$conn=mysql_connect($server,$user,$password) or die("connection failed");
mysql_select_db($database,$conn);
$query_Rsenroll = "SELECT * FROM `tbl_enroll` WHERE `tbl_enroll`.`courseid` ='".$_GET['courseid']."'";
$result= mysql_query($query_Rsenroll, $conn) or die(mysql_error());
$n=mysql_num_rows($result);
if($n>0)
while($row=mysql_fetch_array($rs))
echo"<option value='$row['courseid']'>$row['courseid']</option>";
mysql_close($conn);
?>

Populating dropdown with sql results

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.

drop down menu from databse no result

im trying to get the value from the database and display them in a dropdown menu
but im getting nothing in the drop down menu, here's the code please anyone ?
<select name="car" value="Select" size="1">
<?php
$sql = "SELECT fullname FROM users";
$result = mysql_query($sql) or die (mysql_error());
while ($row = mysql_fetch_array($result))
{
$name=$row['fullname'];
$options.="<OPTION VALUE=\"$name\">";
}
?>
<option>
<? echo $options ?>
</option>
</select>
You have several errors in your code. Try this instead:
<select name="car" value="Select" size="1">
<?php
$sql = "SELECT fullname FROM users";
$result = mysql_query($sql) or die (mysql_error());
while ($row = mysql_fetch_array($result))
{
//securing from XSS
$name= htmlentities($row['fullname']);
//you had no closing tag, no name
$options.="<OPTION VALUE=\"$name\">$name</option>";
}
//need semicolon, no need for tags
echo $options;
?>
</select>
you missed option full syntax $options.="<OPTION VALUE=\"$name\">";
should be $options.="<OPTION VALUE=\"$name\">$name</OPTION>";
EDIT
<option><!--< not required as you already used in $options-->
<? echo $options ?>
</option><!--< not required as you already used in $options-->
in your code Semicolon(;) is missing in the end of $options:
<option>
<? echo $options ?>
</option>
also remove `value="Select"` with select tag
try this:
<select name="car" size="1">
<?php
$sql = "SELECT fullname FROM users";
$result = mysql_query($sql) or die (mysql_error());
while ($row = mysql_fetch_array($result))
{
$name=$row['fullname'];
echo "<OPTION VALUE='$name'>$options</option>";
}
?>
</select>
you mean
<select name="car" value="Select" size="1">
<? echo $options; ?>
</select>
also $options should be of the following form:
$options .= '<option value="'.$name.'">'.$name.'</option>';
change ur code to this..
<select name='cars'> <?php $sql = "SELECT fullname FROM users";
$result = mysql_query($sql) or die (mysql_error());
while ($row = mysql_fetch_array($result))
{
echo "<OPTION VALUE='".$row['fullname']."'>".$row['fullname']."</OPTION>";
}?></select>
if you have entry in you table just remove extra option from your code
<select name="car" value="Select" size="1">
<?php
$sql = "SELECT fullname FROM users";
$result = mysql_query($sql) or die (mysql_error());
while ($row = mysql_fetch_array($result))
{
$name=$row['fullname'];
$options.="<OPTION VALUE=\"$name\">";
}
?>
<? echo $options ?>
</option>
</select>
just remove <option> before <? echo $options ?>

i am trying to display productname from newproduct mysql table into combobox. The following code is executing but its displaying a blank

**<html>
<body>
<form name="call">
<select name="category">
<option>Select a category</option>
<?php
mysql_connect("localhost","root","");
mysql_select_db("shenvel");
$category = "SELECT productname FROM newproduct";
//retrieving product name alone from newproduct
$query_result = mysql_query($category);
while($result = mysql_fetch_assoc($query_result))
{
?>
<option value = "<?php echo $result['productname']?>"><?php echo $result['productname']?></option>
//the above code displays the combo box with one empty space as output.
**
change fetch records using mysql_fetch_array()
while($result = mysql_fetch_array($query_result))
{
*********
}
And try it..
Try this
<select name="category">
<option>Select a category</option>
<?php
mysql_connect("localhost","root","");
mysql_select_db("shenvel");
$category = "SELECT productname FROM newproduct";
$query_result = mysql_query($category);
while($result = mysql_fetch_array($query_result))
{
?>
<option value = "<?php echo $result['productname']; ?>"><?php echo $result['productname']; ?></option>
<?php
}
?>
</select>
try this:
<html>
<body>
<form name="call">
<select name="category">
<option>Select a category</option>
<?php
mysql_connect("localhost","root","");
mysql_select_db("shenvel");
$category = "SELECT productname FROM newproduct";
//retrieving product name alone from newproduct
$query_result = mysql_query($category);
while($result = mysql_fetch_array($query_result))
{
?>
<option value = "<?php echo $result['productname']?>"><?php echo $result['productname']?></option>
Your while loop is being fully executed in the first <?php ... ?> snippet. The syntax you used for your while loop will not execute across multiple php code snippets.
Try echoing out your option HTML code and thus having only one php code snippet.
<html>
<body>
<form name="call">
<select name="category">
<option>Select a category</option>
<?php
mysql_connect("localhost","root","");
mysql_select_db("shenvel");
$category = "SELECT productname FROM newproduct";
//retrieving product name alone from newproduct
$query_result = mysql_query($category);
while ($result = mysql_fetch_assoc($query_result)) {
echo '<option value = "', $result['productname'], '">', $result['productname'], '</option>';
}
?>

Categories