I've been working on this for at least a month, and can't find this question elsewhere. I know I'm just missing something stupid, but...
There are 2 tables: biz and bizclass. bizclass.bizclassName holds the 250+ classifications to populate the dropdown box. Population from bizclass table and update to biz table both work, but when I try to select the current data from biz.bizClass1 , the dropdown initial value is set as the null value "Select Class". Please help. It's driving me bonkers. I'm too old for many more sleepless nights! Newbie - somewhat familiar with php and javascript but don't have a grip on ajax yet.
echo "Class1: <select name ='bizClass1' id='bizClass1'/> ";
$sql = 'SELECT bizclassName FROM bizclass ORDER BY bizclassName';
$query2 = 'SELECT `bizClass1` FROM `biz` WHERE `bizID` = "'. $search .'"';
$clist = mysqli_query($connection,$sql);
$num=mysqli_num_rows($clist);
$olist = mysqli_query($connection, $query2); // select bizClass from biz
$bizTblRecord = mysqli_fetch_assoc($clist); // option values from bizclass table to populate the dropdown
$row2 = mysqli_fetch_assoc($olist); // fetched the bizClass from biz
if ($row2['bizClass1'] == $bizTblRecord['bizclassName']){
printf ("<option value='%s' selected >%s</option> ", $row2['bizClass1'], $row2['bizClass1']);
} else {
printf ("<option value=''>Select Class</option> ");
}//end if
for($numrows=1; $numrows<= $num; $numrows++)
{
// Associative array
$row=mysqli_fetch_assoc($clist);
printf ("<option value='%s'>%s</option>",$row['bizclassName'],$row['bizclassName']);
} //end for
echo "</select>";
// Free result set
mysqli_free_result($clist);
mysqli_free_result($olist);
echo "Class1: <select name ='bizClass1' id='bizClass1'/>
<option value=''>Select Class</option> ";
$sql = 'SELECT bizclassName FROM bizclass ORDER BY bizclassName';
$clist = mysqli_query($connection,$sql);
$num=mysqli_num_rows($clist);
$querySEARCH = 'SELECT `bizClass1` FROM `biz` WHERE `bizID` = "'. $search .'"';
$SEARCHlist = mysqli_query($connection, $querySEARCH); // select bizClass from biz
$rowSEARCH= mysqli_fetch_assoc($SEARCHlist); // fetched the bizClass from biz
for($numrows=1; $numrows<= $num; $numrows++)
{
// Associative array
$row=mysqli_fetch_assoc($clist);
if ($rowSEARCH['bizClass1'] == $row['bizclassName']){
printf ("<option value='%s' selected >%s</option> ", $rowSEARCH['bizClass1'], $rowSEARCH['bizClass1']);
} else {
printf ("<option value='%s'>%s</option>",$row['bizclassName'],$row['bizclassName']);
}//end if
} //end for
echo "</select>";
// Free result set
mysqli_free_result($clist);
mysqli_free_result($SEARCHlist);
If statement have to be inside the loop to compare each option with search.
Test tables:
biz
bizID bizClass1
1 ronaldo
2 shevshenko
3 falcao
4 zidane
5 valderrama
bizclass
bizclassName
falcao
ozil
ronaldo
messi
shevshenko
valderrama
hazard
totti
Related
Update: I’m now getting an error telling me that roomID is null and this is why it’s not going into the database. Can you see why it would be null when it should be the option that the user selects in the select box?
I'm working on a hotel booking system as an exercise. I am trying to make a form that will insert a new booking into the database. I have the customer and room IDS in the mySQL database and I have put them into HTML select boxes for the user to select an option.
This works fine until I try to save the new booking to the database. I don't get any errors and it tells me the booking has been saved on my page but if I go into the database it hasn't been saved. I have tried inserting the customerID and roomID values from text boxes so just typing it in instead of the drop down menu and that inserts into the database just fine.
I'm pretty sure the problem is something to do with getting the value out of the select box and into the POST variable but I'm not 100% sure. I've only been coding for 6 months so I don't really know what I'm doing! I've tried to figure it out by looking at other examples on here but nothing seems to work.
Here's my code:
Getting the data to fill the select boxes:
//Locate room names
$query = $query = 'SELECT `roomID` FROM `room`';
$result2 = mysqli_query($DBC,$query);
$rowcount = mysqli_num_rows($result2);
if ($rowcount > 0) {
$row = mysqli_fetch_assoc($result2);}
//Locate customerIDs
$query = $query = 'SELECT `customerID` FROM `customer`';
$result = mysqli_query($DBC,$query);
$rowcount = mysqli_num_rows($result);
if ($rowcount > 0) {
$row = mysqli_fetch_assoc($result);}
Creating Select boxes:
<form method="POST" action="newbooking.php">
<p>
<label for="customerID">Customer ID: </label>
<?php
echo "<select name='customerID'id='customerID' input type='number'>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<option value='customerIDselect'>" . $row['customerID'] . "</option>";
//customerID
$customerID = cleanInput($_POST['customerIDselect']);
}
echo "</select>";
?>
</p>
<p>
<label for="rooms">Room (name, type, beds): </label>
<?php
echo "<select name='roomID'id='roomID' input type='number'>";
// output data of each row
while($row = $result2->fetch_assoc()) {
echo "<option value='roomIDselect'>" . $row['roomID'] . "</option>";
//roomID
$roomID = cleanInput($_POST['roomIDselect']);
}
echo "</select>";
?>
</p>
Inserting data into database:
//save the customer data if the error flag is still clear
if ($error == 0) {
$query = 'INSERT INTO `bookings` (`customerID`,`roomID`,`checkin`,`checkout`, `extras`) VALUES (?,?,?,?,?)';
$stmt = mysqli_prepare($DBC,$query); //prepare the query
mysqli_stmt_bind_param($stmt,'sssss', $customerID, $roomID, $checkin, $checkout, $extras);
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
echo "<h2>booking saved</h2>";
} else {
echo "<h2>$msg</h2>".PHP_EOL;
}
I am currently working on a small project. I have to retrieve some data from a DB (MySQL) and insert it into a webpage as a select (Drop Down box). The code I have written in PHP is:
<?php
// Connect to the db.
require ('mysqli_connect.php');
// Make the query:
$q = "SELECT employee_name from employee where dept_id=3 ORDER BY employee_id ASC";
// Run the query.
$r = mysqli_query ($dbc, $q);
if ($r) // If it ran OK, display the records.
{
echo '<select name="employee_name">';
// Fetch and print all the records:
while ($row = mysqli_fetch_array($r))
{
echo '<option value="'.$row['employee_name'] . '>"'.$row['employee_name'] .'</option>';
}
echo "</select>";
}
mysqli_free_result ($r); // Free up the resources.
mysqli_close($dbc); // Close the database connection.
?>
When I execute the query in MySQL console, it returns the correct output. [It is a list of five names].
Can you help me find the error?
Echo your end select out of the loop.
while ($row = mysqli_fetch_array($r))
{
echo '<option value="'.$row['employee_name'] . '>"'.$row['employee_name'] .'</option>';
}
echo "</select>";
Use this:
if ($r = #mysqli_query ($dbc, $q))
{
echo 'select ....
As $r is different to true on SELECT queries.
EDIT
You are closing the select tag on every iteration of the while. Try it like this:
if ($r = #mysqli_query ($dbc, $q))
{
echo '<select name="employee_name">';
// Fetch and print all the records:
while ($row = mysqli_fetch_array($r))
{
echo '<option value="'.$row['employee_name'] . '>"'.$row['employee_name'] .'</option>';
}
echo "</select>";
// ^
// |__ Now the </select> is out of the loop
}
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();
}
I'm searching a way to retrieve an ID corresponding to a 'select' without execute any other query when I select an item from Database:
I use the select item in a form.
Here is the way I select some names from a table from Database:
$sql = "SELECT ID, Name,Surname FROM Table;";
$result = mysql_query($sql);
if(!$result) die ('Unable to run query:'.mysql_error());
$la = "<SELECT name='names'>";
$la .= "<OPTION selected='selected' disabled='disabled' >Choose a name</OPTION>";
while(list($id, $name) = mysql_fetch_row($result)) {
$selectnames .= "<OPTION >$name</OPTION>";
}
$selectnames .= "</SELECT>";
I want to know the ID corresponding to the '$selectnames' I select from a form,
Thanks!
You need to set the value of the option to $id:
while(list($id, $name) = mysql_fetch_row($result)) {
$selectnames .= "<OPTION value='$id'>$name</OPTION>";
}
And then when the form is posted you can check $_POST['names'] to get the ID. As you might have noticed, if you don't specify the value then $_POST['name'] will contain the $name value rather than the $id value.
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>";
?>