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>";
}
Related
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>";
?>
Need some help on this PHP and MySQL query.
The selection box gets populated properly but for selecting the current set default value I cant seem to get the if statement it to work. been trying to find an answer everywhere to this question but cant seem to get it.
What I'm trying to do is to just select the latest row from the table TOP and to see if the field BRAND equals the SN field from the table BRANDS
hope someone can shed some light on my failings here cause I'm doing my head in.
<select name="top">
<?php
$query = $db->query("SELECT * FROM BRANDS");
$querytop = $db->query("SELECT MAX(NUM) FROM TOP");
$rtop = $querytop->fetch_object();
while($row = $query->fetch_object()){
if ($row->SN == $rtop->BRAND){
echo "<option value='".$row->SN."' selected=\"selected\">".$row->BRAND."</option>";
}else{
echo "<option value='".$row->SN."'>".$row->BRAND."</option>";
}
}
?>
</select>
EDIT1:
Here's the HTML I'm getting; the $rtop->BRAND does not return a value. therefore the if statement is always false, even if it; logically speaking, should return true.
<select name="top">
<option value='21'>asd</option>
<option value='22'>Test1</option>
</select>
I think, this shoud help you:
$querytop = $db->query("SELECT MAX(NUM) FROM TOP");
change to:
$querytop = $db->query("SELECT MAX(NUM) AS BRAND FROM TOP");
Please Try This type..:)
if ($row->SN == $rtop->BRAND){
echo "<option value='{$row->SN}' selected=\"selected\">"{$row->BRAND}"</option>";
}else{
echo "<option value='{$row->SN}'>"{$row->BRAND}"</option>";
}
the problem I faced was that I was only selecting the NUM field when im actually trying to find the BRAND value from the num field.
the correction
$querytop = $db->query("SELECT BRAND FROM toppage WHERE NUM=(SELECT max(NUM) FROM toppage)");
the code in full
<?php
$query = $db->query("SELECT * FROM BRANDS");
$querytop = $db->query("SELECT BRAND FROM toppage WHERE NUM=(SELECT max(NUM) FROM toppage)");
$rtop = $querytop->fetch_object();
while($row = $query->fetch_object()){
if ($row->SN == $rtop->BRAND){
echo "<option value='$row->SN' selected=\"selected\">".$row->BRAND."</option>";
}else{
echo "<option value='$row->SN'>".$row->BRAND."</option>";
}
}
?>
I am trying to retrieve multiple cars from a car rental database based on the model, so if someone clicks on Ford it would retrieve all cars that have a Model ID of 2 for example. The current code I have only shows the first record in the database, how do I make a while loop that would echo the rows for each match?
$ModelID = $_GET['model_id'];
$result = mysqli_query($con, "SELECT RegNumber, Colour FROM Car WHERE ModelID = '$ModelID'");
$row = $result->fetch_assoc();
echo $row["RegNumber"];
echo $row["Colour"];
You need something like:
while ($row = $result->fetch_assoc()) {
echo $row['RegNumber'];
echo $row['Colour'];
}
For more details, please go through the PHP Documentation
One possible solution is using mysqli::query. And never forget to escape data inside the query, in that case you should use mysqli::real_escape_string function
$escapedModelId = $mysqli->real_escape_string($ModelID);
$query= "SELECT RegNumber, Colour FROM Car WHERE ModelID = '$escapedModelId'";
if ($result = $mysqli->query($query)) {
while ($row = $result->fetch_assoc()) {
echo $row["RegNumber"];
echo $row["Colour"];
}
}
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);
}
I want to use the following code to populate a drop-down list with all of the customer types:
<select name="type" id="type" class="neutral">
<?php // SQL QUERY TO RETRIEVE EVERY TYPE OF CUSTOMER
$sql = "SELECT CUST_TYPE FROM `CUSTOMER` GROUP BY `CUST_TYPE`";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result)){echo '<option value="'.$row.'">'.$row.'</option>';}
?>
The query works in phpMyAdmin; it gives the correct output (corporate, other, school, sports) but in the webpage it displays a drop-down list with 4 options, all containing the word "array." Please help!
Try
while($row = mysql_fetch_assoc($result)){
echo '<option value="'.$row['CUST_TYPE'].'">'.$row['CUST_TYPE'].'</option>';
}