Auto Populate Select Box depending on previous input - php

I have a select box which shows job roles.
Upon select I would like another select box to auto populate the department (stored in the same row as job role in the MySQL DB)
<select class="full-width"name="manager" data-placeholder="Manager" data-init-plugin="select2">
<?
$result=mysql_query("SELECT * FROM hr_job_roles")or die('Could not connect:' . mysql_error());
for ($i = 1; $i <= mysql_num_rows($result); $i++)
{
$row = mysql_fetch_array($result);
$title=$row['title'];
echo" <option value='$title'>$title </option>";
}
if ($i % 4 == 0) {
echo ''; // it's time no move to next row
}
?>
</select>

Related

Sending options from html select box into database

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

SELECT OPTION from mysql field value

I need to populate an html SELECT OPTION using a while loop with an sequence from 1 to the value of a integer number that is stored in a field called dummy3 inside a table from mysql database.
Example: if the dummy3 field has number 4, than the select option should display 1 2 3 4 as option values.
Here is my code, that is not working. The option values in this code version always come empty (with other code variants i tried the browser looped forever).
Here is my code:
echo 'Quantidade: '.'<select name="product_qty" id="product_qty" class="btn">';
$qt = 1;
$quantidade = "SELECT dummy3 FROM shop_products ORDER BY category ASC";
$quantidade1 = mysqli_query($conn, $quantidade) or die("database error:". mysqli_error($conn));
while($row_quantidade = mysqli_fetch_assoc($quantidade1, MYSQL_NUM)){
$row_qt = $row_quantidade['dummy3'];
while($qt <= $row_quantidade['dummy3']){
echo '<option value="'.$qt.'">'.$qt.'</option>';
}
}
echo '</select>';
echo 'Quantidade: '.'<select name="product_qty" id="product_qty" class="btn">';
$qt = 1;
$quantidade = "SELECT dummy3 FROM shop_products ORDER BY category ASC";
$quantidade1 = mysqli_query($conn, $quantidade) or die("database error:". mysqli_error($conn));
while($row_quantidade = mysqli_fetch_assoc($quantidade1, MYSQL_NUM)){
$row_qt = $row_quantidade['dummy3'];
for($i = 1; $i <= $row_qt; $i++) {
echo '<option value="'.$i.'">'.$i.'</option>';
}
}
echo '</select>';
Does this work?

bootstrap select option multiple selection not inserting all selected options

I have bootstrap chosen-select multiple select option. It is wokring fine but when i go to insert multiple selections into database it doesnt insert all selected options it inserts only the biggest value from the selected options.
Select Box
<select name="type" data-placeholder="Choose a Card Type..." class="chosen-select" style="text-align: left;" tabindex="2" multiple>
<?php
$q_all_categories = mysql_query("SELECT * from sort_kcc");
while ($all_categories = mysql_fetch_array($q_all_categories)) {
$category_id = $all_categories['id'];
$categories_name = $all_categories['ename'];
$categories_aname = $all_categories['aname'];
$q_selected_cat = mysql_query("
SELECT * FROM rel_sort_kcc WHERE sort_id=".$category_id."
AND sr_id=".$_GET['id']."
");
$selected = "";
while ($category = mysql_fetch_array($q_selected_cat)) {
$selected_category = $category['sort_id'];
if($category_id == $selected_category){$selected = "selected";}
}
print "<option class='ur' ".$selected." value='".$category_id."'>".$categories_name."</option>";
$selected = "";
}
?>
</select>
Insert Query
$categoriesCT = $_POST['type'];
for($i=0; $i < count($categoriesCT); $i++) {
mysql_query("
INSERT INTO
rel_sort_kcc
(sr_id, sort_id)
VALUES
('".$_POST['idName']."', '".$categoriesCT[$i]."')
");
}

select a value in dropdown list from two database tables

I want to edit the employees's information in a dropdown list, I have two database tables where there is a shared field :
in table1, I have many fields and one of them is the employee Position which is a number. in table2 I have two fields: EmpPos(which is equal to Position in table1) and PosName.
Now, in the dropdown list, when I add a new employee I fill the list with PosName from table2 but store Position number in table1.
the problem is in the edit form, I print all employee's info from table1 in the form to edit them but I don't know how to select employee's PosName from its associated Position in the dropdown list
here is my code:
echo" <b>Position: </b> <select name='Position' >";
$sql="SELECT * FROM table1 LEFT JOIN table2 ON table1.Position = table2.EmpPos";
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
while($row = $result->fetch_assoc()) {
$PosName=$row["PosName"];
$Id=$row['EmpPos'];
echo" <option name= '$PosName' value='$PosName' ' . (($Id==$Position) ? 'selected' : '') . '>$PosName</option>";
}
}
thanks
Write query to get all position from position table:
$sql="SELECT * FROM positionTable order by position name asc";
$result = $conn->query($sql);
$Empquery = "Fetch all data related to that employee.";
echo" <b>Position: </b> <select name='Position' >";
if ($result->num_rows > 0)
{
while($row = $result->fetch_assoc()) {
$PosName=$row["PosName"];
$Id=$row['EmpPos']; //it must emp's position id fetch from other query.
echo" <option name= '$PosName' value='$PosName' ' . (($Id==$Position) ? 'selected' : '') . '>$PosName</option>";
}
}
echo "</select>";

Displaying mysqli_fetch array results in dropdown php

I have a drop down that is being populated from a static select. Then when a choice is made in the first drop down a query runs and the second drop down is populated from the database depending on selection in first select box. Here is the code. I'm having a problem displaying the second drop down with the data.
$selectvalue = mysqli_real_escape_string($mysqli, $_GET['selectvalue']);
$result = mysqli_query($mysqli, "SELECT DISTINCT '$selectvalue' FROM accounts ");
echo '<option value="">Please select...</option>';
while($row = mysqli_fetch_array($result))
{
echo '<option value="'.$row['$selectvalue'].'">' . $row['$selectvalue'] . "</option>";
//echo $row['drink_type'] ."<br/>";
}
mysqli_free_result($result);
mysqli_close($connection);
?>
<?php
$selectvalue = mysqli_real_escape_string($mysqli, $_GET['selectvalue']);
$result = mysqli_query($mysqli, "SELECT DISTINCT * FROM accounts WHERE col_name = '".$selectvalue."' ");
echo '
<select name="some_name">
<option value="">Please select...</option>';
while($row = mysqli_fetch_array($result)) {
echo '<option value="'.$row['col_name'].'">'.$row['col_name']."</option>";
}
mysqli_free_result($result);
mysqli_close($connection);
?>

Categories