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
Related
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>';
?>
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.
I'm making a web site for a business and in the "car list" I would like to take information from 2 tables of my data base, one for the car info (name, price...) and the other one for the images URL table.
Example of my code:
$mysqli = new mysqli("localhost","root","","database") or die("1");
$sql = "SELECT * FROM cars WHERE type= '".$cartype."'";
$result = $mysqli->query($sql);
if($result)
{
while($row = $result->fetch_assoc())
{
?>
<table>
<tr>
<td> HERE IMAGE URL FROM TABLE 2 </td>
<td> <?php echo $row['name']; ?> INFO FROM TABLE 1 </td>
</td>
</table>
<?php
}
}
How can I connect to table 2 and put the info in my while loop?
Thanks a lot.
You are going to have to join your two database tables in your select query. To do that you need an id in your photos table that links it to your cars table.
Example:
$sql = "SELECT name,url FROM cars,photos WHERE cars.id = photos.car_id AND type= '".$cartype."'";
Then use
$row['name']; $row['url'];
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>
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>';