populating dropdown using php and mysql where clause not working - php

im trying a basic thing but there is some problem with my query, Im trying to populate names in a dropdown where status = Arrived, however the query doesn't work with where clause, neither giving any syntax error. Here is my code:
$sql= mysqli_query($con,"Select name from reservations where status='Arrived'");
$result=mysqli_fetch_assoc($sql);
echo "<select name=\"name\">";
while($row = mysqli_fetch_array($sql))
{
echo "<option value='".$row['name']."'>".$row['name']."</option>";
}
echo "</select>";
?>

Related

store the country ID from country drop down box in mysql but it is storing as 0 in db

//This is my php to view dropdown box <?php
include('connect.php');
//for retriving data from DB and show it in drop down box
$query="SELECT cname FROM country";
$result = mysqli_query ($con, "$query");
echo "<select name=country value=''>";
while($r=mysqli_fetch_row($result))
{
echo "<option value='$r[0]'> $r[0] </option>";
}
echo "</select>";
?>
But when i am storing in DB my country_id is always 0.
You are not fetching country id from SQL Query
Change
$query="SELECT cname FROM country";
To
$query="SELECT cid, cname FROM country"; // Update cid with your country id field
Therefore $r[0] is getting blank value.
Use this code for your reference
//This is my php to view dropdown box
<?php
include('connect.php');
//for retriving data from DB and show it in drop down box
$query="SELECT countryid,cname FROM country";
$result = mysqli_query ($con, "$query");
echo "<select name='country'>";
while($r=mysqli_fetch_row($result))
{
echo "<option value='$r[countryid]'> $r[cname] </option>";
}
echo "</select>";
?>
I am going with Pupil's answer to change the query to
$query="SELECT cid, cname FROM country"; // Update cid with your country id field
and then change this
echo "<select name=country value=''>";
to
echo "<select name=country>"; //remove value=''
EDIT:
Please check your column type may be it is "INT" when you try to store text it stores 0.

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

Drop down box filled with mysql rows php

I am currently working on a school project. My goal is to develop a dynamic web page that allows people to retrieve data from a database. I want to create a few drop down boxes that allows users to narrow down the data from my database I have created.
For example, I have "year" as one column in my database because I have gathered data from multiple years. I would like to establish a way for users to select a specific year by using an HTML drop down box. How exactly do I go about coding something like this using PHP and my database?
Here is my code so far, but I can't seem to get anywhere with this.
<select name='year'>
<?php
$query = "select distinct year from test order by year";
$result = $result->query($query);
while ($row = $result->fetch_assoc()) {
echo "<option value='".$row->year."'>".$row->year."</option>";
}
?>
</select>
With this code, I am getting a drop down box, but no choices are given. It is blank. Any ideas? I would appreciate any help.
fetch_assoc() return the associative array not an object,should be accessed like array
$query = "select distinct year from test order by year";
$result = $result->query($query);
while ($row = $result->fetch_assoc()) {
echo "<option value='".$row['year']."'>".$row['year']."</option>";
}
<select name='year'>
<?php
$query = "select distinct year from test order by year";
$result = $result->query($query);
while ($row = $result->fetch_object()) {
echo "<option value='".$row->year."'>".$row->year."</option>";
}
?>
</select>
To access the result as objects you should use fetch_object.
So your loop should change as below:
while ($row = $result->fetch_object()) {
Or use
$sql=mysql_query("select distinct year from test order by year");
while ($row = mysql_fetch_assoc($sql)) {
echo "<option value='".$row["year"]."'>".$row["year"]."</option>";
}

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();
}

Echo table field values to a dropdown in 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>";

Categories