displaying data from a table into a drop down menu - php

i have 2 tables
domains_info and tb2
i have got the form working well and entering data into the database
here is the top of my page
<?php
$action = isset($_POST['action']) ? $_POST['action'] : "";
if($action=='create'){
//include database connection
include 'db_connect.php';
//write query
$query = "insert into domains_info
set
domain = '".$mysqli->real_escape_string($_POST['domain'])."',
domain_account = '".$mysqli->real_escape_string($_POST['domain_account'])."',
renew_date = '".$mysqli->real_escape_string($_POST['renew_date'])."'";
if( $mysqli->query($query) ) {
//if saving success
header("Location:domains.php");
}else{
echo "Database Error: Unable to create record.";
}
$mysqli->close();
}
here is the form
<select id="domain_account" name="domain_account" class="txtBox">
<option value="">-select-</option>
<option value="a">a</option>
<option value="b">b</option>
<option value="c">c</option>
</select>
i tried and changed the top of my page like this
<?php
$action = isset($_POST['action']) ? $_POST['action'] : "";
if($action=='create'){
//include database connection
include 'db_connect.php';
//write query
$query = "insert into domains_info
set
domain = '".$mysqli->real_escape_string($_POST['domain'])."',
domain_account = '".$mysqli->real_escape_string($_POST['domain_account'])."',
renew_date = '".$mysqli->real_escape_string($_POST['renew_date'])."'";
if( $mysqli->query($query) ) {
//if saving success
header("Location:domains.php");
}else{
echo "Database Error: Unable to create record.";
}
$mysqli->close();
}
$query = "select id, data
from tb2
where id='".$mysqli->real_escape_string($_REQUEST['id'])."'
limit 0,1";
$result = $mysqli->query( $query );
$row = $result->fetch_assoc();
$id = $row['id'];
$data = $row['data'];
and updated my form as this
<select id="domain_account" name="domain_account" class="txtBox">
<option value="">-select-</option>
<option value="<?php echo$data; ?>"><?php echo$data; ?></option>
</select>
as you can tell i am very new to this and its not working.
sorry i didnt explain what i was trying to achieve, i am trying to display a drop down form with data from a database.

Try this for populating the dropdown using database:
<?php
$query = "select id, data from tb2";
$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['data']; ?>"><?php echo $row['data']; ?></option>
<?php
}
echo "</select>";
?>

Related

Keep the value selected after fetching the data and query to insert it into database

I have this onchange event on php, if the user choose an option, it fetched the data that is connected on it. However, the page refreshes and the choice of the user disappear. Also, I cannot insert the data to database. I've tried add a form method="post" to the form attribute, but unfortunately it cannot fetch the data.
Thankyou in advance.
<?php
$dsn = 'mysql:host=localhost;dbname=admin';
$username = 'root';
$password = '';
try{
// Connect To MySQL Database
$con = new PDO($dsn,$username,$password);
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (Exception $ex) {
echo 'Not Connected '.$ex->getMessage();
}
$gradeassign = '';
$sectionassign = '';
function getPosts()
{
$posts = array();
$posts[3] = $_POST['sectionassign'];
$posts[4] = $_POST['gradeassign'];
return $posts;
}
if(isset($_POST['addfac']))
{
$data = getPosts();
$insertStmt = $con->prepare('INSERT INTO
facultyagain(sectionnumber,gradelevelassign)
VALUES(:sectionassign,:gradeassign)');
$insertStmt->execute(array(
':sectionassign'=> $data[3],
':gradeassign'=> $data[4],
));
if($insertStmt)
{
echo 'Data Inserted';
}
}
?>
<html>
<head>
<title>Country</title>
</head>
<body>
<form action="trial.php">
Select Your grade
<select name="gradeassign" onchange="this.form.submit()">
<option value="" disabled selected>--select--</option>
<option value="1">Grade 1</option>
<option value="2">Grade 2</option>
<option value="europe">Europe</option>
</select>
<?php
require 'connection.php';
if(isset($_GET["gradeassign"])){
$gradeassign=$_GET["gradeassign"];
$sql = "SELECT sectionassign FROM sections WHERE gradeassign='$gradeassign'";
$result = $con->query($sql);
echo "<select>";
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<option value='$row[sectionassign]'>" . $row["sectionassign"]. " </option>";
}
} else { echo "<B>0 Results</B>"; }
echo "</select>";
}
?>
<BR>
<button id="addfac" name="addfac">Add Faculty</button>
</form>
</body>
</html>
You can also store the value in a variable and check to see if the option matches the variable. For example:
<option value="" disabled>--select--</option>
<option value="1" <?php if(isset($_GET['gradeassign'])) && $_GET['gradeassign'] == "1") echo "checked"; ?>>Grade 1</option>
<option value="2" <?php if(isset($_GET['gradeassign'])) && $_GET['gradeassign'] == "2") echo "checked"; ?>>Grade 2</option>
<option value="europe" <?php if(isset($_GET['gradeassign'])) && $_GET['gradeassign'] == "europe") echo "checked"; ?>>Europe</option>
The better way to do it would be to create the whole list with a loop. Bu this should work.

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>

echo records from database not displaying?

I have a select option in my index.php and inside the select I echo a record from my database, but it displays this value "--".
How can I display the record from database to display options?
All the code works fine I just want the records from the database to display.
class.user.php
public function getID($ID)
{
$stmt = $this->db->prepare("SELECT * FROM rlbet WHERE ID=:ID");
$stmt->execute(array(":ID"=>$ID));
$editRow=$stmt->fetch(PDO::FETCH_ASSOC);
return $editRow;
}
here is my php
$Reason_for_Deduction = isset($_GET['Reason_for_Deduction']) ? $_GET['Reason_for_Deduction'] : '';
if(isset($_GET['ID']))
{
$ID = $_GET['ID'];
extract($LTID->getID($ID));
Here is my html:
<select name="Province" class="form-control" id="category" onchange="javascript: dynamicdropdown(this.options[this.selectedIndex].value);">
<option selected="selected"value="<?php echo $Province; ?>">--- </option>
<option value="Ajhsfg">Ajhsfg</option>
<option value="dsfdf">dsfdf</option>
<option value="jhfdsg">jhfdsg</option>
<option value="sfg">sfg</option>
<option value="jhsfg">jhsfg</option>
<option value="jhsfg">jhsfg</option>
<option value="jhsdgf">jhsdgf</option>
</select>
as you can see in the image i echoed most of them but when i try to echoed it using the select its not appearing..
try this code
<?php
$sql = "SELECT Province FROM table";
$result = $conn->query($sql);
if ($result->num_rows > 0) { /// output data of each row ?>
<select name="Province" class="form-control" id="category" onchange="javascript: dynamicdropdown(this.options[this.selectedIndex].value);">
<?php while($row = $result->fetch_assoc()) {
?>
<option selected="selected" value="<?php echo $row["Province"]; ?>"> </option>
</select>
<?php
}
} else {
echo "0 results";
}
?>
Do like this:
<select>
<?php while ($data = mysql_fetch_array(mysql_query("your query here"))){ extract($data);?>
<option selected="selected" value="<?php echo $Province; ?>">
<?php echo $Province; ?>
</option>
<?php } ?>
</select>
Try to give the same value for id and name in the Select
<select name="Province" class="form-control" id="Province" onchange="javascript: dynamicdropdown(this.options[this.selectedIndex].value);">
<option selected="selected"value="<?php echo $Province; ?>">--- </option>
<option value="Ajhsfg">Ajhsfg</option>
<option value="dsfdf">dsfdf</option>
<option value="jhfdsg">jhfdsg</option>
<option value="sfg">sfg</option>
<option value="jhsfg">jhsfg</option>
<option value="jhsfg">jhsfg</option>
<option value="jhsdgf">jhsdgf</option>
</select>
<option value="<?php echo $Province; ?>" selected><?php echo $Province; ?></option>
Duplicate the PHP echo between the option tags.
You are probably not getting any data echoed, because you've actually not fetched the data correctly in the first place.
Here's a sneak peek on how to do it correctly:
<?php
function getData() {
// Connect to database
$connection = mysqli_connect("localhost", "username", "password", "database");
// SELECT query to get the value from database
$query = "SELECT * FROM `table` WHERE `Data` = ?";
// Prepare statement, bind the parameter, execute and get result
if ($stmt = mysqli_prepare($connection, $query)) {
mysqli_stmt_bind_param($stmt, "s", $data);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
// If the data entered in the query exists in the database fetch it
if (mysqli_num_rows($result) > 0) {
$data = mysqli_fetch_assoc($result);
echo $data["Data"];
mysqli_stmt_close($stmt);
mysqli_close($connection);
exit;
}
}
}
?>
You must supplant username, password and database with the respective values for your database.
You must supplant Data with the name of the column of your table you are looking to check.
You can supplant $data with a variable name that you like or leave it as it is.
Then you can call the function and have the result echoed:
<option value="<?php getData();?>" selected></option>
try this
<?php
$sql = "SELECT Province FROM table";
$results = mysql_query($sql);
echo"<select>"
foreach($row = mysql_fetch_array($results))
{
echo"<option value ".$row['yourValue'].">".$row['yourValue']."</option>"
}
echo"</select>"

How to insert data in a dropdown list

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

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

Categories