Echo table field values to a dropdown in PHP - php

I've been consulting stack overflow for quite some time now.
I'm developing a system for article management and I encountered a problem.
How do I echo a field values to a dropdown select from a database table?
I have managed to echo it using this:
$result = mysql_query("SELECT catName FROM tblCat");
while($row = mysql_fetch_array($result)){
echo "$row['catName']";
echo "`<br />`";
}
But when I edited the code to this:
$result = mysql_query("SELECT catName FROM tblCat");
while($row = mysql_fetch_array($result)){
echo "`<select>`";
echo "`<option value='{$row['catName']}'>`";
echo "`</select>`";
}
I got no result.

echo "<select>";
$result = mysql_query("SELECT catName FROM tblCat");
while($row = mysql_fetch_array($result))
echo "<option value='{$row['catName']}'>{$row['catName']}</option>";
echo "</select>";

You need to output it twice, you're only setting value, you need the label too:
echo "<option value='{$row['catName']}'>{$row['catName']}</option>";

Related

Filtering database base on a drop-down list that populating from MySQL

I would like to filter some data from MySQL database base on a drop-down list that populating from the MySQL database.
Now I have got a drop-down list like this:
which is populating from the MySQL database:
The following are my code for the drop-down list:
<?php
$result2 = $conn->query("select id, field from customer_details GROUP BY field");
echo "<select id='sf' name='id'>";
echo '<option value="">Show All</option>';
while ($row = $result2->fetch_assoc()) {
unset($id, $field);
$id = $row['id'];
$field = $row['field'];
echo '<option value = "'.$id.'">'.$field.'</option>';
}
echo "</select>";
?>
Does anyone know how can I get the selected text of the drop-down menu?
I have tried to use $_POST[] in different ways like $_POST['id']
<?php
$result2 = $conn->query("select id, field from customer_details GROUP BY field");
echo "<select id='sf' name='id'>";
echo '<option value="">Show All</option>';
while ($row = $result2->fetch_assoc()) {
unset($id, $field);
$id = $row['id'];
$field = $row['field'];
echo '<option value = "'.$id.'">'.$field.'</option>';
}
echo "</select>";
echo $_POST['id'];
?>
then Notice pop up from the website:
Notice: Undefined index: id
What should I do?

PHP Dynamic drop down list for deleting rows

I am creating a deletion page for the removal of rows in the database. I have everything working up until the actual removal of the rows upon submit. The records display in the drop down without problem, any ideas of a solution are appreciated. (I am using deprecated SQL, I know.)
1ST PART OF PHP (FORM)
$query = 'SELECT event_name FROM event';
$result = mysql_query($query);
echo "<select name='events' value='-'>\n";
echo "<option value='Select event'>Select an event to be deleted\n";
while($row = mysql_fetch_row($result))
{
$eventSelect = $row[0];
echo "<option value='$eventSelect'>$eventSelect</option>\n";
}
echo "</select>"
2ND PART OF PHP (DELETION)
if (isset($_POST['eventSelect']))
{
$eventselection = $_POST['eventSelect'];
$query = "DELETE FROM event WHERE event_name = '$eventselection'";
$result = mysql_query($query);
}

Retrieve data from mysql through checkbox

I want to search question from mysql using ComboBox. If I select chapter number one in ComboBox, I want to display that question which only chapter one have.
In this suppose my chapter 1 contains 2 questions, chapter 2 contains some questions and so on. When I select chapter number 1 then it doesn't display question that chapter 1 have. It will only print the last question from the last chapter. How can I solve this problem?
<?php
$sql= "select distinct chapter from math";
$q= mysql_query($sql);
echo "<select name='fname'>";
while($info=mysql_fetch_array($q)){
$d1 = $info['chapter'];
echo "<option> ".$info['chapter']."</option>";
}
echo "</select>";
$sql1 = "select question from math where chapter=$d1";
$sql1_res = mysql_query($sql1) or die(mysql_error());
while($row = mysql_fetch_array($sql1_res)){
$question=htmlspecialchars_decode($row['question'], ENT_QUOTES); // It gives only last question.
echo $question;
}
?>
Your mistake is that you are selecting questions from last iteration of query where you select chapters.
...
while($info=mysql_fetch_array($q)){
$d1 = $info['chapter'];
echo "<option> ".$info['chapter']."</option>";
}
echo "</select>";
$sql1 = "select question from math where chapter=$d1";
...
will always select last chapter. But you have another problem. Assuming you want to show questions when user select some value from dropdown, you have to send that selection using POST/GET/AJAX back to PHP and generate results based on that selection. Something like this:
if(!isset($_POST['fname']))
{
$sql= "select distinct chapter from math";
$q= mysql_query($sql);
echo "<select name='fname'>";
while($info=mysql_fetch_array($q)){
echo "<option> ".$info['chapter']."</option>";
}
echo "</select>";
}
else
{
$sql1 = "select question from math where chapter = " . $_POST['fname'];
$sql1_res = mysql_query($sql1) or die(mysql_error());
while($row = mysql_fetch_array($sql1_res)){
$question=htmlspecialchars_decode($row['question'], ENT_QUOTES); // It gives only last question.
echo $question;
}
}
In your question query change the while loop according to below code:
<?php
$sql= "select distinct chapter from math";
$q= mysql_query($sql);
echo "<select name='fname'>";
$d1 = array();
while($info=mysql_fetch_array($q)){
$d1[] = $info['chapter'];
echo "<option> ".$info['chapter']."</option>";
}
echo "</select>";
$sql1 = "select question from math where chapter IN ('".implode("','",$d1)."')";
$sql1_res = mysql_query($sql1) or die(mysql_error());
while($row = mysql_fetch_array($sql1_res)){
$question=htmlspecialchars_decode($row['question'], ENT_QUOTES); // It gives only last question.
echo $question;
}
?>
make the $d1 variable as an array and change the select query of question.
<?php
$sql= "select distinct chapter from math";
$q= mysql_query($sql);
echo "<select name='fname'>";
while($info=mysql_fetch_array($q))
{
$d1 = $info['chapter'];
echo "<option> ".$info['chapter']."</option>";
echo "</select>";
$sql1 = "select question from math where chapter=$d1";
$sql1_res = mysql_query($sql1) or die(mysql_error());
while($row = mysql_fetch_array($sql1_res))
{
$question=htmlspecialchars_decode($row['question'], ENT_QUOTES); // It gives only last question.
echo $question;
}
}
?>
$sql1 = "select question from math where chapter=".$d1;

Why is my drop down list not populating with the table data?

WHy is my drop down list not populating with the table data? (dropdown box is empty)
And what is used to display data upon selection of an item in that drop down - is it a "VIEW" (please do provide a study link so I can learn)
My Code
<?php
$con=mysqli_connect("localhost","root","","ismat_db");
//check connection
if(mysqli_errno($con))
{
echo "Can't Connect to mySQL:".mysqli_connect_error();
}
else
{
echo "Connected to mySQL</br>";
}
//$query = 'SELECT FirstName FROM persons';
//$result = mysqli_query($con,$query);
$query = mysqli_query($con,"SELECT 'FirstName' FROM persons");
//print_r($query);
//echo '<select name="FirstName">';
echo "<select name= 'FirstName'>";
//while($row=mysqli_fetch_array($result))
while($row=mysqli_fetch_array($query))
{
echo $row;
//echo "<option value='".$row['FirstName']."'>".'</option>';
}
echo '</select>';
?>
You had 2 errors:
I pointed the first in the comment: to print an option you must use this code:
echo "<option value='". $row['FirstName']."'>".$row['FirstName']
. '</option>';
The second is in your SQL: you are not selecting the FirstName field from the database, but a string 'FirstName' instead. That's why it is printed twice as you said. Use this SQL to get the field:
$query = mysqli_query($con,"SELECT FirstName FROM persons");
Also usually people put an id of the record and not a field, that may have possible duplicates into the value of an <option>. So, I would have used:
echo "<option value='". $row['id']."'>".$row['FirstName']
. '</option>';
selecting the id from the database together with first name.
Try this:
echo "<option value='".$row['FirstName']."'>".$row['FirstName']."</option>";
Also seems that you are having an issue with the database query. Swap your while loop with the following and see if it works
if ($result = $mysqli->query($query)) {
while ($row = $result->fetch_assoc()) {
echo "<option value='".$row['FirstName']."'>".$row['FirstName']."</option>";
}
$result->free();
}

Keeping lastly selected value from a dropdownlist after button click

Just like the title says i'm having difficulties in achieving it.
Here's my dropdownlist:
<?php
$query = "SELECT data, rel_id FROM $tbl_rel_balansas INNER JOIN $tbl_balansas ON $tbl_rel_balansas.rel_id = $tbl_balansas.id WHERE $tbl_rel_balansas.member_id = '$_SESSION[id]' group by data";
$result = mysql_query ($query);
echo "<select name=data value=''>Data</option>";
while($nt=mysql_fetch_array($result)){
echo "<option value=$nt[data] name=\"blabla\">$nt[data]</option>";
}
echo "</select>";
?>
Here's the buttonclick:
<?php
if(isset($_POST['Submit']))
{
$query = "SELECT SUM(suma), paskirtis FROM $tbl_rel_balansas INNER JOIN $tbl_balansas ON $tbl_rel_balansas.rel_id = $tbl_balansas.id WHERE $tbl_rel_balansas.member_id = '$_SESSION[id]' AND data ='".$_POST['data']."' group by paskirtis";
$result = mysql_query ($query);
echo "<tr><td>Paskirtis:</td><td>Biudzetas:</td><td>Isleista:</td><td>Likutis:</td></tr>";
while($nt=mysql_fetch_array($result)){
if($nt['SUM(suma)'] != null){
$suma = $nt['SUM(suma)'];
}
echo "<tr><td>$nt[paskirtis]</td>
<td><input type=\"text\" name=\"isleista[]\" value=\"Skiriamų pinigų kiekis...\" method=\"post\"></td><td>".$suma." Lt</td><td>--</td></tr> <br>";
}
}
?>
After I press it, it retrieves the data I want from the date I've chosen from the drop down list and also reset whole drop down list showing the first value of the dates from sql database, not the one I selected. If anyone knows how to keep the selected value in the list any help is greatly appriciated!
Try this, you need to place select="selected" in the while loop. See below code how I placed the $selected
<?php
$query = "SELECT data, rel_id FROM $tbl_rel_balansas INNER JOIN $tbl_balansas ON $tbl_rel_balansas.rel_id = $tbl_balansas.id WHERE $tbl_rel_balansas.member_id = '$_SESSION[id]' group by data";
$result = mysql_query ($query);
echo "<select name=data value=''>Data</option>";
while($nt=mysql_fetch_array($result)){
$selected = ($_POST['blabla'] == $nt[data])?'selected="selected"':NULL;
echo "<option value=$nt[data] name=\"blabla\" $selected >$nt[data]</option>";
}
echo "</select>";
?>

Categories