PHP drop down menu from database - php

I'm trying to recreate a make a booking form. They have a drop down select option with 3 values from the room table in the database. In the room table I have the values roomID, roomname, roomtype and beds. I need to have roomname, roomtype and beds displayed as one option in the drop down. So the 3 values will be on the same line if that makes sense. So for example it will say Brown, Double, 2beds and then I can select the options and will view another set of data. I currently know how to make a drop down with only one value in the line but how would I make a drop down menu that presents 3 values from a table?
Below is my code so far:
<?php
include "config.php";
$db = mysqli_connect("127.0.0.1", DBUSER, DBPASSWORD, DBDATABASE);
$query = $db->query("select roomname, roomtype, beds from room");
echo '<select room="roomname">';
while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
echo '<option value="'.$row['roomname'].'">'.$row['roomname'].'</option>';
}
echo '</select>';
?>

I think this is what you are expecting.If you want to show roomname, roomtype, beds in one line just concatenate it.
<?php
include "config.php";
$db = mysqli_connect("127.0.0.1", DBUSER, DBPASSWORD, DBDATABASE);
$query = $db->query("select id,roomname, roomtype, beds from room");
echo '<select name="roomname">';
while ($row = $query->fetch_assoc()) {
echo '<option value="'.$row['id'].'">'.$row['roomname'].'-'.$row['roomtype'].'-'.$row['beds from room'].'</option>';
}
echo '</select>';
?>

Related

Printing of data

I created 3 tables in my database "Colleges" in PhpMyAdmin. The names of the tables are "cool", "data" and "tab". The first table "cool" consists of the the names of the states of India. It has two columns : ID and Statename. From the data in this table, I created a drop down list in HTML form. Further the HTML form consists of the name, email id, contact and the address. The user has to fill in the details and select his/her state from the drop down list. Now, the user input consisting of name, email id, contact and the address goes into the second table "data" and the selected state from the drop down list goes into the 3rd table "tab", "tab" consists of 2 columns ID and stat where the state name gets stored here. I joined the above two tables "data" and "tab" using inner join of SQL. When I fetched the data in another web page, the name, email id, contact and address are getting printed but not the the statename. Instead of the state name, ID of the statename (as given in table cool) is getting printed.
I want state name to get printed.
Here is the drop down list created using the data from my database :
<td>State :</td>
<td>
<?php
$mysqli = new mysqli('localhost', 'root', '', 'colleges');
$resultset = $mysqli->query("SELECT ID, Statename from cool");
?>
<select name="state">
<?php
while($rows = $resultset->fetch_assoc())
{
$ID = $rows['ID'];
$Statename = $rows['Statename'];
echo "<option value='$ID'>$Statename</option>";
}
?>
</select>
</td>
</tr>
And what are the changes to be done here to insert the selected dropdown state name into the table in my database ?
<?php
$connection = mysqli_connect("localhost", "root", "", "colleges");
if(isset($_POST['submit'])){ // Fetching variables of the form which travels in URL
$name = $_POST['name'];
$email = $_POST['email'];
$contact = $_POST['contact'];
$address = $_POST['address'];
$state = $_POST['state'];
if($name !=''||$email !=''){
//Insert Query of SQL
$insert = "INSERT Into data(student_name, student_email, student_contact, student_address) values ('$name', '$email', '$contact', '$address')";
$query = mysqli_query($connection, $insert);
$insert2 = "INSERT Into tab(stat) values ('$state')";
$query2 = mysqli_query($connection, $insert2);
echo "<br/><br/><span>Data has been inserted successfully</span>";
}
else{
echo "<p>Insertion Failed <br/> Some Fields are Blank</p>";
}
}
mysqli_close($connection); // Closing Connection with Server
?>
3). The printing part:
<?php
$hostname = "localhost";
$dbname = "colleges";
$username = "root";
$password = "";
$conn = mysqli_connect("$hostname","$username","","$dbname");
if(mysqli_connect_errno())
{
echo "Failed to Connect MySQL (phpmyadmin) Database: ".mysqli_connect_error();
}
$query = ("select student_name, student_email, student_contact, student_address, stat from data t2 inner join tab t3 on t2.ID=t3.ID");
$result = mysqli_query($conn, $query);
echo "<center>";
echo "<h1>Student list</h1>";
echo "<hr/>";
echo"<table border = '1'>
<tr>
<th>Name</th>
<th>Email</th>
<th>Contact</th>
<th>Address</th>
<th>State</th>
</tr>";
while ($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>".$row['student_name']."</td>";
echo "<td>".$row['student_email']."</td>";
echo "<td>".$row['student_contact']."</td>";
echo "<td>".$row['student_address']."</td>";
echo "<td>".$row['stat']."</td>";
echo "</tr>";
}
echo "</table>";
echo "</center>";
mysqli_close($conn);
?>
echo "<option value='$ID'>$Statename</option>";
and
$state = $_POST['state'];
and
$insert2 = "INSERT Into tab(stat) values ('$state')";
You are actually not inserting the name of the state, but it's id, as the select return the selected option's value, not it's content. You can either change the first line to
echo "<option value='$Statename'>$Statename</option>";
which is a weird solution, or just use a join in your mysql query to get the statename
JOIN ON cool.ID = tab.stat
Extention:
When you use select, the innerHTML of option is displayed, but the value is sent in POST. So if you write
<select name="state">
<option value="1">name</option>
</select>
You will see name name, but the 1 will be sent.
On the next page, $State = $_POST['state']; will have $State the value of '1'. In the SQL you put this value to your stat field in your database, with a generated ID. It means, your ID won't hold any data, but the state field will!
So when printing on the third page, you have to join the list of states by it's ID with the stored stateid in the 'state' field. Then you will be able to print the statenames.
"SELECT * FROM t2 LEFT JOIN t3 ON t2.?? = t3.?? LEFT JOIN t1 ON t1.ID = t3.state"
Or something similar. Do you have a field you can use to join the t2 and t3 tables? It seems you are losing data when inserting to your database.

How to show drop down selected values

I am trying to create a dropdown menu which has values from the database and when selected it should show all the information that are connected to the selected value.
In MySQL I have three tables, person, address and resume. I created two drop-down menu's(see code below) which are showing the values from address > address_state and address > address_city. What needs to happen is when I selected a state it will show only the persons living in the selected state. And when selecting city it has to show all the persons living in the same city.
My drop-down menu select state only:(got the same code city)
$servername = "localhost";
$username = "root";
$password = "usbw";
$dbname = "persons";
// CREATE A CONNECTION WITH THE DATABASE
// CONNECTIE MAKEN MET DATABASE
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//$sql="SELECT name,id FROM student";
$sql="SELECT DISTINCT address_state FROM address ORDER BY address_state asc";
/* You can add order by clause to the sql statement if the names are to be displayed in alphabetical order */
echo "<select name=address value=''>Student Name</option>"; // list box select command
foreach ($conn->query($sql) as $row){//Array or records stored in $row
echo "<option value=>$row[address_state]</option>";
/* Option values are added by looping through the array */
}
echo "</select>";// Closing of list box
?>
I know you cannot provide any code because I don't have any code to show which actually "selects" the persons. But I have searched on Google for tutorials and I did not find one which did the thing I needed. So I hope you guys can help me or can provide any tutorials.
Replace this line:
echo "<option value=>$row[address_state]</option>";
With
echo "<option value='".$row['address_state']."'>".$row['address_state']."</option>";
First replace:
echo "<select name=address value=''>Student Name</option>";
To:
echo '<select name="address">';
Next replace this:
echo "<option value=>$row[address_state]</option>";
With:
echo '<option value="'.$row[address_state].'">'.$row[address_state].'</option>';

Send file to element in the database

I am working on a website with a database with many tables.
A table called "agency" includes a column for the names of the persons called (E_NAME) and other information in other columns.
There is another table called "tasks" for the tasks that the employee should perform.
How can I show the employees' names in a list and the responsible task presented in his account fter selecting the employee name?
This is the picture of the agency table:
This is the picture of the tasks table:
This is the PHP code for the selection:
<p>task for :
<?php
$dbLink = new mysqli('localhost', 'root', '', 'taskproject');
if(mysqli_connect_errno()) {
die("MySQL connection failed: ". mysqli_connect_error());
}
$query = "SELECT E_NAME FROM agency";
$data = $dbLink->query($query);
// $data = mysqli_query($dbLink,$query);
?>
<select id="emp_name" name="mylist" onchange="selectedvalue(this.value)">
<?php
while($fetch_options = mysqli_fetch_array($data)) { //Loop all the options retrieved from the query
?>
<option id ="<?php echo $fetch_options['ID']; ?>" value="<?php echo $fetch_options['E_NAME']; ?>"><?php echo $fetch_options['E_NAME']; ?></option><!--Echo out options-->
<?php
}
?>
</select>
I did not find any relationship between your agency and the tasks table. I think you need a field in your task table named something like agency_id. Then after selecting the employee name, you can search in your task table with your employee_id:
SELECT * FROM `task` WHERE agency_id = employee_id LIMIT 1

Multiple select form element from MySQL recordset in PHP update form

I have table called empicons that has two columns: EmpNo, IconId
I have the EmpNo stored as a session called $EmpNo
I have another table called icons that has 3 columns: Id, Name, URL
Currently I have a recordset that selects the icons table Id and Name and presents it in a Multiple Select box that then updates the empicons table.
Recordset query: SELECT id, name FROM gbl_icons ORDER BY id ASC
Multiple Select Form Element:
<select name="icons[]" size="10" multiple="multiple">
<?php
do {
?>
<option value="<?php echo $row_icons['id']?>"><?php echo $row_icons['name']?></option>
<?php
} while ($row_icons = mysql_fetch_assoc($icons));
$rows = mysql_num_rows($icons);
if($rows > 0) {
mysql_data_seek($icons, 0);
$row_icons = mysql_fetch_assoc($icons);
}
?>
</select>
I'm trying to do a join with empicons and icons table so that the Multiple Select highlights the data elements found in empicons based on the EmpNo variable. I cannot get the join to work correctly and then update the form element to highlight these.
Try this. It uses mysqli_ functions. If you're having a problem with JOINing the two tables, then we need to work on your query.
<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT id, name FROM gbl_icons ORDER BY id ASC";
echo '<select name="icons[]" size="10" multiple="multiple">';
if ($result = mysqli_query($link, $query)) {
/* fetch associative array */
while ($row = mysqli_fetch_assoc($result)) {
echo '<option value="' . $row[id] . '">' . $row[name] . '</option>';
}
/* free result set */
mysqli_free_result($result);
}
echo '</select>';
/* close connection */
mysqli_close($link);
?>
Your query should be like this
SELECT i.id, i.name, e.EmpNo FROM gbl_icons i
LEFT JOIN empicons e ON e.IconId = i.id AND e.EmpNo = '$EmpNo'
ORDER BY i.id ASC
So you will get all icons from your 'gbl_icons' table and for each one of them a corresponding record in empicons for the current EmpNo. If there is no corresponding record you will get NULL in the 'EmpNo' column.
And your code should be like that
<select name="icons[]" size="10" multiple="multiple">
<?php
while($row_icons = mysqli_fetch_assoc($icons)) {
?>
<option value="<?=$row_icons['id']?>" <?=!empty($row_icons['EmpNo']) ? ' selected="selected"' : ''?><?=$row_icons['name']?></option>
<?php
}
?>
</select>

drop down box displays user name, on selection gets user number

I am creating a report function were a manager can look up amendments to a user rota which is a mySQL table. This contains 5 columns Unique_ID ( Auto Increment), UserID(13 digit number), Date effected(Unix date), Date added(Unix date), Added_by(13 digit number).
To select the the correct data i was populating a drop down from another table which contains username and UserID ( a 13 digit number) however i am stuck with how to select the name ( as no one can remember all staff numbers) and it POST the corresponding user id to the next query.
<form action="<?=$_SERVER['php_SELF']?>" method="get">
<?
$loc = $usersite->UserLocation();
$username = "Uname";
$password = "Pword";
$hostname = "Hname";
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
$selected = mysql_select_db("DBname",$dbhandle)
or die("Could not select database");
$query="SELECT name FROM `login1st` WHERE `Location` = '$loc'";
$result = mysql_query ($query);
echo "<select name=category value=''></option>";
while($nt=mysql_fetch_array($result)){
echo'<option value="'.$nt['name'].'">'.$nt['name'].'</option>';
}
echo "</select>";
?>
<input type="submit" value="Search" class="buttons2">
</form>
this work fine and i have a drop down box with all staff names, is there a way to get the associated userid from the selection to a separate query? E.G
SELECT * FROM `$loc` WHERE `User_id` = (query result from drop down)
i am using the PHP_SELF as the result, I am hoping, will go into a table below the drop down. i am open to any other ways of doing this. Thanks in advance.
in the value-section you can define what to submit. So just change your query to get the UserId as well
$query="SELECT UserID, name FROM `login1st` WHERE `Location` = '$loc'";
and then just output the id in your selectbox:
echo'<option value="'.$nt['UserID'].'">'.$nt['name'].'</option>';

Categories