/    Sign up×
Community /Pin to ProfileBookmark

PHP/MySQL Booking Form

I am currently running a simple php/mysql booking system which allows users to book blocks of time (see attached image).

I want to add the option for users to book:

All Morning,
All Afternoon,
All Evening, or All Day.

So, it should not allow users to book anything if “All Day” is already booked, or shouldnt allow users to book period 1, period 2, break or perido 3 if “all morning” is already booked.

Here is the code i am currently running, which allows users to book individual blocks and stops other users from booking a resource which is already booked at that time on that day.

[code=php]<h1>Make Booking</h1>
<?
//initilize PHP

if($_POST[‘submit’]) //If submit is hit
{

if ($resource==”Select Resource” ) {
die(‘You did not select a resource, please check and <A HREF=”javascript:history.go(-1)”>Try again</A>’);
}
if ($period==”Select Period” ) {
die(‘You did not select a period, please check and <A HREF=”javascript:history.go(-1)”>Try again</A>’);
}
if ($date==”” ) {
die(‘You did not select a date, please check and <A HREF=”javascript:history.go(-1)”>Try again</A>’);
}
if ($name==”” ) {
die(‘You did not enter your name, please check and <A HREF=”javascript:history.go(-1)”>Try again</A>’);
}
if ($comments==”” ) {
die(‘You did not enter a reason for this booking, please check and <A HREF=”javascript:history.go(-1)”>Try again</A>’);
}

//Insert the values into the correct database with the right fields
//mysql table = news
//table columns = id, title, message, who, date, time
//post variables = $title, $message, ‘$who, $date, $time
$fields = “id, resource, period, date, name, comments”;

$result = “INSERT INTO $tblname ($fields) VALUES (‘NULL’, ‘$resource’, ‘$period’, ‘$date’, ‘$name’, ‘$comments’)”;
// * Check for duplicates.
$request = “SELECT * FROM $tblname”;
$db_result = mysql_query($request);
$article = mysql_fetch_object($db_result);

$num_rows = mysql_num_rows($db_result);
$row_count = 0;

while ($row_count < $num_rows) { // While I haven’t checked all the rows.
$article = mysql_fetch_object($db_result); // Put each object into $article.
$row_count++; // * And add one to row count.
if ($article->resource == $resource) { // If session equates to session…
if ($article->period == $period) {
if ($article->date == $date) {

echo “The booking you specified is already taken. <a href=’javascript:history.go(-1)’>Try again</a>”;
exit();
}
}
}
}
$add_record = mysql_query($result);
$str10 = substr($nstory, 0, 50);

//confirm
echo “<h2>Booking Complete</h2> <br /><strong>Resource:</strong> $resource
<br /><strong>Period:</strong> $period
<br /><strong>Date:</strong> $date
<br /><strong>Name:</strong> $name
<br /><strong>Comments:</strong> $comments
<br />
<br /><a href=’makebooking.php’><strong>Click here to make another Booking…</strong></a>”;
}

else
{
// close php so we can put in our code
?>[/code]

Any help would be greatly appreciated.

[upl-file uuid=502ce68d-f829-4282-b5ab-c27a8d0e7345 size=49kB]booking.gif[/upl-file]

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@dankellysauthorMar 15.2011 — Can anyone offer me any advise at all? its driving me mad ?
Copy linkTweet thisAlerts:
@NogDogMar 15.2011 — I would probably create a DB table that defines the individual periods, having columns something like: id, period_name, start_time, end_time. Then there would be a reservation_to_period table, which would need 3 columns: event_date, reservation_id and period_id, with one row for each period to be reserved for a given reservation event. [i]Then[/i] if a user selects to reserve a block of periods (e.g. "all morning") you would check to see if any of the periods are in use, something like:
<i>
</i>SELECT COUNT(*) FROM <span><code>reservation_to_period</code></span> WHERE <span><code>event_date</code></span> = '2011-03-15<span><code> AND </code></span>period_id<span><code> &amp;gt;= 1 AND </code></span>period_id` &lt;= 4;

If the count() value is >0, then you know that block is not available. You could then set up a switch() or other logic to create the WHERE clause based on which block the user chose. If it comes back with a count of zero, then you can go ahead and insert a row for each relevant period for the selected block/date. (Sounds like you may need some table locking to prevent race conditions between competing requests. ? )
×

Success!

Help @dankellys spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...