How to insert data in a dropdown list - php

We managed to get the drop down list menu however, we are having difficulties getting the data from sql. So far, this is what we got.
<select>
<option id="">--Select jobscope--</option>
<?php
$con=mysqli_connect("host","user","password","database");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$getIT = mysqli_query("SELECT job_title FROM `job_details`");
while($viewIT = mysqli_fetch_array($getIT)) {
}
?>
<option id="<?php echo $viewIT['job_title']?>"<?php echo $viewIT['job_title']?></option>
</select>

Shouldn't be like this ? with tag inside WHILE LOOP
while($viewIT = mysql_fetch_array($getIT)) {
<option id="<?php echo $viewIT['job_title']?>"<?php echo $viewIT['job_title']?></option>
}

$query = "SELECT * FROM test_groups_tb WHERE user_id='$userid'";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_assoc($result))
{
$dd .= "<option value='{$row['group_id']}'>{$row['group_name']}</option>";
}

Try this,
<option value="">--select--</option>
<?php
while($rec = mysql_fetch_assoc($result)) {
?>
<option value="<?=$rec['job_title']?>"><?=$rec['job_title']?></option>
<?php }
}?>
</select>

I am not from php background. Try this.
<?php
$query = "SELECT job_title FROM job_details";
$result = $mysqli->query( $query );
echo '<select id="domain_account" name="domain_account" class="txtBox">';
echo '<option value="">-select-</option>';
while ($row = $result->fetch_assoc()){
?>
<option value="<?php echo $row['job_title']; ?>"><?php echo $row['job_title']; ?></option>
<?php
}
echo "</select>";
?>

Better use PDO or MYSQLi . MYSQL* is depriciated
U need to use that <option id="<?php echo $viewIT['job_title']?>"<?php echo $viewIT['job_title']?></option> line b/w while loop
$getIT = mysql_query("SELECT job_title FROM `job_details`");
while($viewIT = mysql_fetch_array($getIT)) {?>
<option id="<?php echo $viewIT['job_title']?>"<?php echo $viewIT['job_title']?></option>
<?hp }?>

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

Display text from a mysql table like a array

<?php
mysql_query("SELECT * FROM page where page_id = '$page_id'") or die(mysql_error());
while($land = mysql_fetch_array($resultxx)){
$land =$land['land'];
?>
output $land is NL,DE,BE,AL..
How is it possible to splits the output
example
<?php
echo "
<select>
<option value='NL'>NL</option>
<option value='DE'>DE</option>
<option value='BE'>BE</option>
<option value='AL'>AL</option>
</select>";
}
?>
Try This
<?php
mysql_query("SELECT * FROM page where page_id = '$page_id'") or die(mysql_error());
while($land = mysql_fetch_array($resultxx)){
$land =$land['land'];
$landarray = explode(",",$land);
$count = count($landarray);
echo "<select>";
for($i=0;$i<$count;$i++){
echo '<option value='.$landarray[$i].'>'.$landarray[$i].'</option>';
}
echo "</select>";
}
?>

Select Form: Database request many times

I have a database that I need to use to do select.
The select is repeat many times (days of the week).
I can't do a loop for because it can have some "gap" in the id so I use the while.
The problem is that for each select I need to call the database again and I think it's not really a good solution. I would like to find another solution which isn't so "heavy"
Here is my code:
<select name="monday">
<option value="none">Monday </option>
<?php
$Requete2 = "SELECT * FROM `record`";
$Result2 = mysql_query($Requete2) or die(mysql_error());
$rows2 = mysql_num_rows($Result2);
while($row2 = mysql_fetch_array($Result2)){ ?>
<option value="<?php echo ($row2['id'] - 1); ?>"><?php echo $row2['name']; ?></option>
<?php } ?>
</select>
<select name="tuesday">
<option value="none">Tuesday </option>
<?php
$Requete2 = "SELECT * FROM `record`";
$Result2 = mysql_query($Requete2) or die(mysql_error());
$rows2 = mysql_num_rows($Result2);
while($row2 = mysql_fetch_array($Result2)){ ?>
<option value="<?php echo ($row2['id'] - 1); ?>"><?php echo $row2['name']; ?></option>
<?php } ?>
</select>
Thanks
If you want to print out the same for every day you can use this(I havn't tested it for syntax errors):
<?php
$sql_query = "SELECT * FROM `record`";
$query = mysql_query($sql_query) or die(mysql_error());
$optionHtml = '';
while($row = mysql_fetch_array($query)){
$optionHtml .= '<option value="' . ($row["id"] - 1) . '">' . $row["name"] . '</option>';
}
?>
<select name="monday">
<option value="none">Monday</option>
<?php echo $optionHtml; ?>
</select>
<select name="tuesday">
<option value="none">Tuesday</option>
<?php echo $optionHtml; ?>
</select>

keep the selected value for dropdown after submit

How do I keep the selected item after I submit the page? I have the country dropdown list with the following code.
<?php
$SQL = "SELECT countr_id, country_name FROM countries";
$Result = mysql_query($SQL) or die(mysql_error());
?>
<select name="country" style="width:400px"> <option value='-1'></option>
<?php
while($row = mysql_fetch_array($Result))
{
echo "<option value=\"".$row["country_id"]."\">".$row["country_name"]."</option>";
}
?>
<?php
$SQL = "SELECT countr_id, country_name FROM countries";
$Result = mysql_query($SQL) or die(mysql_error());
?>
<select name="country" style="width:400px"> <option value='-1'></option>
<?php
while($row = mysql_fetch_array($Result))
{
echo "<option value=\"".$row["country_id"]."\"";
if($_POST['country'] == $row['country_id'])
echo 'selected';
echo ">".$row["country_name"]."</option>";
}
?>
<?php
while($row = mysql_fetch_array($Result))
{
if ($_POST['country'] == $row["country_id"]) {
echo "<option value=\"".$row["country_id"]."\" selected="selected">".$row["country_name"]."</option>";
} else {
echo "<option value=\"".$row["country_id"]."\">".$row["country_name"]."</option>";
}
}
?>
<?php
$SQL = "SELECT countr_id, country_name FROM countries";
$Result = mysql_query($SQL) or die(mysql_error());
?>
<select name="country" style="width:400px"> <option value='-1'></option>
<?php
while($row = mysql_fetch_array($Result))
{
echo "<option ";
if($_REQUEST['country'] == $row["country_id"]) echo 'selected="selected" ';
echo "value=\"".$row["country_id"]."\">".$row["country_name"]."</option>";
}
?>
This code depends if you are trying to keep the value after you submit back to the original page.
<?php
$SQL = "SELECT countr_id, country_name FROM countries";
$Result = mysql_query($SQL) or die(mysql_error());
?>
<select name="country" style="width:400px">
<option value='-1'></option>
<?php
while($row = mysql_fetch_array($Result))
{
echo "<option ";
if($_REQUEST["yourSelectName"] ==$row["country_id"])
echo ' selected = "selected" ';
echo " value=\"".$row["country_id"]."\">".$row["country_name"]."</option>";
}
?>
</select>
Or you can do it all inline... less code, doesn't require all the if/then/else stuff
Change
echo "<option value=\"".$row["country_id"]."\">".$row["country_name"]."</option>";
To
echo "<option ". (($_POST['country'] == $row["country_id"]) ? 'selected ' : '') ."value=\"".$row["country_id"]."\">".$row["country_name"]."</option>";
If it is get (passed in URL), the use GET
<?php
$selectedid = 5; //example of selected if before submitting
$SQL = "SELECT countr_id, country_name FROM countries";
$Result = mysql_query($SQL) or die(mysql_error());
?>
<select name="country" style="width:400px"> <option value='-1'></option>
<?php
while($row = mysql_fetch_array($Result))
{
echo "<option value=\"".$row["country_id"]." ".(($selected==$row["country_id"])?"SELECTED":"")."\">".$row["country_name"]."</option>";
}
?>
//assume you have $result = array(your result list);
<select name='question'>
<?php
foreach ($result as $question) {
if ($_POST['question'] == $question) {
$selected = "selected";
} else {
$selected = '';
}
echo "<option value='" . $question . "' $selected>$question</option>";
}
?>
</select>

Categories