echo records from database not displaying? - php

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

Related

how to concatenate two columns from mysql in PHP

I'm trying to get all the names from MySQL into a dropdown list using PHP.
I connected to MySQL using PDO. currently I can get only the first name, But I want the names to be first name + last name in the drop down list but I couldn't concatenate them.
I tried to concatenate them like this:
<select class="un">
<option class="op" value="" disabled selected style="color:gray">Username</option>
<?php foreach ($result as $output) { ?>
<option class="op"> <?php echo $output["firstname"+"lastname"]; ?></option>
<?php } ?>
</select>
but that didn't work out for me.
$query="select * from user_details";
$exec = $conn->prepare($query);
$exec->execute();
$rc = $exec->rowCount();
$result=$exec->fetchAll();
<select class="un">
<option class="op" value="" disabled selected style="color:gray">Username</option>
<?php foreach ($result as $output) { ?>
<option class="op"> <?php echo $output["firstname"]; ?></option>
<?php } ?>
</select>
it worked only with one column which is the firstname but I want it to be both firstname + lastname
Update this view code:
<select class="un">
<option class="op" value="" disabled selected style="color:gray">Username</option>
<?php foreach ($result as $output) { ?>
<option class="op"> <?php echo $output["firstname"] .' '.$output["lastname"]; ?></option>
<?php } ?>
</select>
With Mysql you can try this concat()
$query="select concat(firstname,' ',lastname) as fullname from user_details";
$exec = $conn->prepare($query);
$exec->execute();
$rc = $exec->rowCount();
$result=$exec->fetchAll();
<select class="un">
<option class="op" value="" disabled selected style="color:gray">Username</option>
<?php foreach ($result as $output) { ?>
<option class="op"> <?php echo $output["fullname"]; ?></option>
<?php } ?>
</select>

how to display specific fields from db using the selected value in dropdown listbox in php

<select id="qualification" class="form-control">
<option selected="selected">SELECT</option>
<option value="pick"</option>
<?php $sql = mysqli_query($con, "SELECT DISTINCT qualification From
enquire");
$row = mysqli_num_rows($sql);
while ($row = mysqli_fetch_array($sql))
{ echo "<option value='". $row['qualification'] ."'>".$row['qualification']."</option>" ; } ?>
</select>
how to retrieve fields in table format from database using qualification ?
<?php
$sql="SELECT Option1, option2 FROM yourTable";
$result = mysqli_query($YourDBconnection,$sql);
?>
your SELECT like
<select>
<?php
while ($row = mysql_fetch_assoc($result)){
echo'<option>'.$row['option1'].'<option>';
}
?>
</select>
Do this
<select id="qualification" class="form-control">
<option selected="selected">SELECT</option>
<option value="pick"</option>
<?php $sql = mysqli_query($con, "SELECT DISTINCT qualification From
enquire");
//$row = mysqli_num_rows($sql);
while ($row = mysql_fetch_assoc($sql)))
{ ?>
<option value="<?php echo $row['qualification']; ?>"> <?php echo $row['qualification']; ?> </option>
<?php } ?>
</select>
You may wanna reconsider your codes and use prepared statement to avoid SQL injection.
The below code is prepared statement
<select id="qualification" class="form-control">
<option selected="selected">SELECT</option>
<option value="pick"</option>
<?php $sql =$con->prepare("SELECT DISTINCT qualification From
enquire");
$sql->execute();
$result = $sql->get_result();
while ($row = $result->fetch_assoc())
{ ?>
<option value="<?php echo $row['qualification']; ?>"> <?php echo $row['qualification']; ?> </option>
<?php } ?>
</select>

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>

Setting selected option in drop-down box

My SELECT looks like the following:
<?php
$query = "SELECT * FROM Rec_SW2_Rel AS a JOIN SW2 b ON a.Sbj_ID = b.IDsbj GROUP BY a.Sbj_ID ORDER BY b.Descriptor";
$result = mysql_query($query);
?>
<select name="country" onchange="getState(this.value)">
<?php
while ($line = mysql_fetch_array($result, MYSQL_ASSOC))
{
?>
<option value="<?php echo $line['Sbj_ID']; ?>">
<?php echo $line['Descriptor']; ?>
</option>
<?php
}
mysql_close();
?>
</select>
Querying the DB and setting up the drop-down works. The problem is that the value listed first isn't automatically selected. If a user wants to use it, for further navigation, they must first select a different one and then select the first once again.
I couldn't alter the values in the DB. If I insert selected='selected' it returns the last value of the result set, but always without being selected.
You maybe want this? First selected option when the form is loaded is blank.
<select name="country" onchange="getState(this.value)">
<option value=""></option>
<?php
while ($line = mysql_fetch_array($result, MYSQL_ASSOC))
{
or select the selected data from the database? selected column with selected value.
<select name="country" onchange="getState(this.value)">
<?php
$first = true;
while ($line = mysql_fetch_array($result, MYSQL_ASSOC))
{
?>
<option value="<?php echo $line['Sbj_ID']; ?>" <?php echo ($line['selected']=='selected') ? 'selected="selected"' : '' ; ?>>
you can test with respect to $line['Sbj_ID'] if this is = to the value you want by default
<?php
$query = "SELECT * FROM Rec_SW2_Rel AS a JOIN SW2 b ON a.Sbj_ID = b.IDsbj GROUP BY a.Sbj_ID ORDER BY b.Descriptor";
$result = mysql_query($query);
?>
<select name="country" onchange="getState(this.value)">
<?php
while ($line = mysql_fetch_array($result, MYSQL_ASSOC))
{
?>
<option value="<?php echo $line['Sbj_ID']; ?>" <?php if($line['Sbj_ID']==value_you_want_selected){?>selected<?php } ?>>
<?php echo $line['Descriptor']; ?>
</option>
<?php
$i++; }
mysql_close();
?>

How to populate a particular column list in Combo Box with PHP?

I am using the given code below to populate some values from a column of a table. It's just getting filled blank..
Can you please find the problem with it ?
<select name="category">
<option value="" selected>Select a category</option>
<?php
mysql_connect("localhost","root","");
mysql_select_db("muskilaasaan");
$category = "SELECT cat FROM category";
$query_result = mysql_query($category);
while($result = mysql_fetch_array($query_result))
{
?>
<option value = "<?php echo $result['cat']?>"/>
<?php
}
?>
</select>
<select name="category">
<option value="" selected>Select a category</option>
<?php
mysql_connect("localhost","root","");
mysql_select_db("muskilaasaan");
$category = "SELECT cat FROM category";
$query_result = mysql_query($category);
while($result = mysql_fetch_assoc($query_result))
{
?>
<option value = "<?php echo $result['cat']?>"><?php echo $result['cat']?></option>
<?php
}
?>
</select>
Changed to mysql_fetch_assoc and also you didn't put anything in the option tags, that would cause it to appear blank.
it does not help if you specify the connection string as a second argument in mysql_select_db() ? see here for an example: http://php.net/manual/de/function.mysql-select-db.php

Categories