0

I'm making a restaurant reservation module using PHP.

The problem is that I would like to make the user choose which time they would like to come and which time the would like to go.

Between these two times the table needs to be occupied, so there needs to be a name in that cell between those times.

I have no idea how to get this done.

Here is my existing code:

$day="0".$_POST['day'];
$month="0".$_POST['month'];
$year=$_POST['year'];
$date=$year ."-". $month ."-". $day;
$table=$_POST['table'];
$name=$_POST['name'];
$fromtime=$_POST['fromtime'];
$untiltime=$_POST['untiltime'];
$tablenumber = $table;

$host="localhost";
$user="root";
$password="root";
$database="restaurant";
$connection = mysql_connect ($host, $user, $password)
    or die ("could not connect with server");
$db = mysql_select_db ($database, $connection)
    or die ("could not connect with db");

$query = mysql_query("SELECT * FROM tables WHERE table='$tablenumber' AND date = '$date'");
?>
3

1 Answer 1

1

I guess you have a one to many relationship with a table for the reservations (if they can only reserve 1 table), so you need an insert statement to add a reservation

Here some pseudo code

INSERT INTO reservation (tableid, startTime,endtime,reservationName) VALUES (tableid, yourFronttime, yourEndTime, reservationName)

but before inserting you have to check if the table is free like this

SELECT tableId
FROM reservation 
WHERE (startTime BETWEEN yourFrontTime AND untilTime) OR (endtime BETWEEN yourFrontTime AND untilTime) 
AND TableId = yourId

If you got results with that select they got to choose another time or another table.

Sign up to request clarification or add additional context in comments.

2 Comments

Why on Earth must you SELECT * to see if records exist? A simple SELECT COUNT(*) would suffice.
it's only an example... I don't think they will copy/paste this speudoCode... You don't even need a Count(*).. you could select any column and it's only for guidance, I didn't even concat or use a prepared statement for the parameters. But I will edit my answer just in case. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.