I'm really struggling with some php code. It's for an assignment at university. I'm designing a booking system for vip areas within a nightclub. I want to be able to show the available areas in a table so then I can select that area and proceed to book that area for the date searched.
Basically I've managed to write some code and I think I'm getting there but I'm just a little stuck now. I've attached the availablebooking.php file which I want to be able to pull the areas from the database that aren't currently booked for the date searched. What I've tried to do is pull the area_id and room_name where the area_id is not in the booking table on the date searched, resulting in the areas that are displayed all being available. But the code I've managed to write displays the opposite, so it displays the rooms that are booked, not the rooms that are available.
Just looking for some guidance.
availablebooking.php
$Criteria = $_POST ['criteria'];
$query = "SELECT Area.Area_id, Room.Room_name FROM Booking_details
INNER JOIN Area ON Area.Area_id=Booking_details.Area_id
INNER JOIN Room ON Room.Room_id=Area.Room_id
INNER JOIN Booking ON Booking_details.Booking_id=Booking.Booking_id
WHERE Booking.Date != $Criteria";
$result = mysqli_query($dbcon, $query) or die("Error getting data");
echo "<table>";
echo "<tr> <th> Area</th> <th> Room </th>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr><td>";
echo $row['Area_id'];
echo "</td><td>";
echo $row['Room_name'];
echo "</td></tr>";
}
SELECT a.Area_id
, r.Room_name
FROM Booking_details d
JOIN Area a
ON a.Area_id = d.Area_id
JOIN Room r
ON r.Room_id = a.Room_id
LEFT JOIN Booking b
ON d.Booking_id = b.Booking_id
AND b.Date = '$Criteria'
WHERE b.date IS NULL;
Or something like that
Related
I have a problem with this MySQL Statment. So basicly i have a table called games and i want to
display this table on my website. Table games has Foregin Keys like developer_id,
publisher_id, categorie_id, platform1_id, platform2_id, platform3_id, platform4_id, platform5_id.
I have 5 of platform because in Table platforms i have 5 records (PC, PS4, XB1, SWITCH, MOBILE). (If you guys know any better and easier solution to these platforms pls tell me.)
So now my output on my website works but for example instead of showing developer name it shows
it's ID. I know i have to INNER JOIN them and i probbably can INNER JOIN all foregin keys but the
platform one. Because i dont know how to INNER JOIN if you have more then one FK from one table.
If you need more info tell me. I will also include picture of my DB and my PHP code where i SELECT from table games.
$query = "SELECT * FROM games WHERE id = $id";
$result = mysqli_query($link, $query) or die(mysqli_error($link));
while ($row = mysqli_fetch_array($result)) {
echo '<tr>';
echo '<td>'.$row['developer_id'].'</td>';
echo '<td>'.$row['publisher_id'].'</td>';
echo '<td>'.$row['categorie_id'].'</td>';
echo '<td>'.$row['platform1_id'].'</td>';
echo '<td>'.$row['platform2_id'].'</td>';
echo '<td>'.$row['platform3_id'].'</td>';
echo '<td>'.$row['platform4_id'].'</td>';
echo '<td>'.$row['platform5_id'].'</td>';
echo '<td>'.$row['game_name'].'</td>';
echo '<td>'.$row['relese_date'].'</td>';
$intro = $row['introduction'];
$desc = $row['description'];
echo '<td>'.$row['rating'].'</td>';
echo '</tr>';
}
Picture of my DataBase
SELECT games.game_name, games.relese_date, games.introduction, games.rating, games.description, dev.name as developer, pub.name as publisher, (SELECT * FROM platforms WHERE platforms.id in (games.platform1_id,games.platform2_id,games.platform3_id,games.platform4_id,games.platform5_id)) as plats
FROM games
INNER JOIN developers AS dev ON dev.id = games.developer_id
INNER JOIN publishers AS pub ON pub.id = games.publisher_id
WHERE games.id = $id
Keep in mind that the platforms will be listed as a collection under the property plats, and each of those items will be an object of properties as.
E.X.
return object=>
developer_id
publisher_id
categorie_id
plats => [
platform1_id=>name,
platform2_id=>name,
platform3_id=>name,
platform4_id=>name,
platform5_id=>name
]
game_name
relese_date
introduction
description
rating
I'm trying to create a fixture list for a football competition currently, and i have been stuck with trying to display something like "team1name V team2name".The tables that i am trying to pull the data from are:
Team
teamID
teamname
Fixtures
hometeam
awayteam
The sql query I have written up is:
$sql = "SELECT homeTeam, awayTeam, roundID, teamID, teamName, logo, groundName, abbreviatedName, matchDate, matchTime, venue
FROM fixtures
INNER JOIN team ON fixtures.homeTeam = team.teamID
INNER JOIN ground ON ground.groundID = team.groundID";
$results = mysqli_query($conn, $sql)
or die ('Problem with query' . mysqli_error());
and the PHP code which i have been using to display this data is:
<?php
while ($row = mysqli_fetch_array($results))
{
?>
<tr>
<td><?php echo $row ["teamName"]?></td>
<td> V </td>
<td><?php echo $row ["teamName"]></td>
</tr>
<?php
}
mysqli_close($conn);
?>
I understand i am calling teamname twice and hence why i am getting the same name displayed, but i am unsure as to how to get my code to differentiate the two IDs and names. Any help would be greatly appreciated
This is just a SQL problem. You need to join team twice, once for home team and once for away team.
SELECT homeTeamID, awayTeamID, homeTeam.teamID homeTeamID,
awayTeam.teamID awayTeamID, homeTeam.teamName homeTeamName,
awayTeam.teamName awayTeamName
FROM fixtures
INNER JOIN team homeTeam ON fixtures.homeTeam = homeTeam.teamID
INNER JOIN team awayTeam ON fixtures.awayTeam = awayTeam.teamID
INNER JOIN ground ON ground.groundID = team.groundID
By naming the selects, you can use the separately in your PHP code.
<?php echo $row['homeTeamName']; ?> v <?php echo $row['awayTeamName']; ?>
So I am trying to populate a drop down by grabbing my playersID from the players table whilst sharing the same playersID with my Payments table.
When I put my query as below, it works fine.
$sql = "SELECT playersID FROM players";
But when I want to join the two tables together, I get an empty drop down.
<?php
include "dbconnect.php";
$sql = "SELECT players.playersID FROM players INNER JOIN Payments ON Payments.playersID = players.playersID";
if (!$result = mysql_query($sql, $conn))
{
die('Error in querying the database' . mysql_error());
}
echo "<br><select name = 'listbox' id = 'listbox' onclick = 'populate()'>";
while ($row = mysql_fetch_array($result))
{
$playersID = $row['playersID'];
$allText = "$playersID,";
echo "<option value = '$allText'> $playersID</option>";
}
echo "</select>";
mysql_close($conn); ?>
Note I have never done an INNER JOIN before, so it may be a simple issue. The aim of the code is to enter payment information into a form and save the data to the data table. I want to show the playerID in the payments table to show that particular player paid for this product.
Cheers!
It looks like you want to LEFT OUTER JOIN with the payments table instead of inner join, because the payment table does not have a payment for all players yet or for any at all.
I want to display the employee's leaves which is same department with me. I only want the employee which same department with me but the output show all of the employee in database
Here is my database
table leave:{Leave_ID(PK), Data_Apply, Leave_Type, Status, Emp_ID(FK)}
table employee: {Emp_ID(PK), Emp_Name, Dept_ID(FK)}
table department: {Dept_ID(PK), Dept_Name, Dept_Desc}
As a example I'm head of department of Marketing and I want to see employee's leave detail who under me and in a same department. I tried to use function in_array to display but fail.
Here is my code
<?php
//$test = mysql_query("select * from employee");
//if(in_array($search["Dept_ID"], array($test)))
$result = mysql_query("select * from `leave`");
if ($result == FALSE)
{
die(mysql_error());
}
while($row = mysql_fetch_assoc($result))
{
?>
<tr>
<td><?php echo $row["Leave_ID"];?></td>
<td><?php echo $row["Emp_ID"];?></td>
<td><?php echo $row["Date_Apply"];?></td>
<td><?php echo $row["Leave_Type"];?></td>
<td><?php echo $row["Status"];?></td>
<td>Profile</td>
</tr>
<?php
}
?>
Is there is any function or anything as a suggestion to used. I'm a newbie in programming and sorry for my bad english
$department_id = 4;
$result = mysql_query(' select l.*
from leave as l
join employee as e
on l.emp_id = e.emp_id
where e.dept_id = '.mysql_real_escape_string($department_id));
$department_name = 'this one';
$result = mysql_query(" select l.*
from leave as l
join employee as e
on l.emp_id = e.emp_id
join department as e
on d.dept_id = e.dept_id
where d.dept_name like '%".mysql_real_escape_string($department_name))."%'");
edit
After reading the first comment down there, I think you're saying that you essentially have an employee_id and you want to filter the query by that employee's department. So... here's some code to do that:
http://sqlfiddle.com/#!2/41bf7/1/0
There are two queries there... they're about the same so I would just choose which ever is easier for you to understand. You would add them to the PHP like this (using the first query from the sql fiddle):
$logged_in_employee_id = 1;
$result = mysql_query('select e.emp_id, e.emp_name,
l.date_apply, l.leave_type, l.status,
d.dept_name, d.dept_desc
from `leave` as l
join employee as e
on l.emp_id = e.emp_id
join department as d
on d.dept_id = e.dept_id
where d.dept_id in (
select dd.dept_id
from employee as ee
join department as dd
on dd.dept_id = ee.dept_id
where ee.emp_id = '.mysql_real_escape_string($logged_in_employee_id)).' )');
I'm not sure where you're getting the employee_id or the department_id but make sure you sanitize and validate anything you put into a query like this. I am using mysql_real_escape_string which helps but that query will still break if someone hijacks your POST data (or something) and uses a string instead of an integer value. There are some great posts on StackOverflow about how to do this; just search for sanitizing input, sql injection with PHP, and how to do prepared statements or use PDO.
Try to change your query (at the moment it takes all the records in "leave" table).
Assuming that you know the code of your department, you can use something similar (not tested):
SELECT leave.*, employee.Emp_Name, department.Dept_Name, department.Dept_Desc
FROM leave, employee, department
WHERE leave.Emp_ID=employee.Emp_ID
AND department.Dept_ID=employee.Dept_ID
AND department.Dept_ID="<your department ID>"
So I'm building a car booking website. And there is a cars tables that is like this:
Cars
CarID
CarModel
CarMake
Registration
And also a reservations table like this:
Reservations:
ReservationID
CarID
StartDate
EndDate
So When a user inputs the dates which they would like to book a car I query my reservations table:
If the dates are already in the reservation table I want to get that car ID, and then exclude that car from the list the user is shown, so they can not book it.
My problem is that I have multiple cars in the Database that are the same mode and make but have a different CarID and Registration.
I also group the cars by model so that a user is only shown one car of a certain type.
$carstring = mysql_query("SELECT * FROM cars {$statement} AND deleted = 'no'" GROUP BY CarModel);
$getcars = $carstring;
while($searchcars = mysql_fetch_array($getcars)) {
$checkreservations = mysql_query("SELECT * FROM reservations WHERE startDate = '".$sqlcoldate."' and carID = '".$searchcars['carID']."'");
$thiscar_num_rows = mysql_num_rows($checkreservations);
So as you can see at the minute I can tell which cars are taken in the reservations table, and I can echo out true or false from the num_rows
However I think it is the wrong way around because what I want to do is find out which cars by CarID are already taken, and then exclude them from the $getcars query loop that displays all the cars to the user, then group by model.
Can anyone tell me a way to do this, or even a better way to go about it?
An easy way to exclude the cars that are reserved is awith a subquery that gets all cars that ARE reserved and than stating in the main query that those cars are not allowed with the CarID NOT IN construction
<?php
// select all cars that are not reserved at sqlcoldate
$sql = "SELECT *
FROM Cars
WHERE CarID NOT IN (
SELECT CarID
FROM Reservations
WHERE StartDate > '".$sqlcoldate."' and EndDate < '".$sqlcoldate."'
)
GROUP BY CarModel";
// execute that query
$result = mysql_query($sql);
// if there are no results print a message
if (mysql_num_rows($result) == 0) {
echo "No cars found";
exit; // Exit the function, because there is nothing to do
}
// else print all available cars
while ($row = mysql_fetch_assoc($result)) {
echo "Model available car is :" . $row["CarModel"] . " </br>";
}
?>
Didn't actualy test it. But it should work
SELECT c.* FROM cars c
LEFT JOIN reservations r
ON c.carID=r.carID AND
selected_date BETWEEN r.startDate AND r.endDate
WHERE r.carID is null