I have a booking form set up where the user can book different facilities but I can't get my head around how to prevent the user from booking a date that is before today or how to prevent them from double booking.
I did try and work with a datepicker but I am using the HTML5 standard datepicker and also using Twitter Bootstrap but I don't know how to restrict dates from the user.
Below includes my full form and the PHP included in it.
<?php
include "config.php";
//Booking point
if(isset($_POST['booking']))
{
//get values for variables
$pitchID = $_POST['pitchID'];
$start_date = $_POST['start_date'];
$start_hour = $_POST['start_hour'];
$end_hour = $_POST['end_hour'];
$booking_age = $_POST['booking_age'];
$pitch_size = $_POST['pitch_size'];
$light_tokens = $_POST['light_tokens'];
$q = $db->prepare("INSERT INTO booking SET pitchID = ?, start_date = ?, start_hour = ?, end_hour = ?, booking_age = ?, pitch_size = ?, light_tokens = ?");
$query = $q->execute(array($pitchID,$start_date,$start_hour,$end_hour,$booking_age,$pitch_size,$light_tokens));
$count = $q->rowCount();
if($count == 0)
{
echo "Your booking has been made";
header("Location:home2_template.html");
return;
}else {
echo "That booking already exists";
}
}
?>
To check if a date is before a certain date you could convert your dates to timestamp and check if one is lower than the other
$start_date = $_POST['start_date'];
if( strtotime($start_date) < time()) {
// Date is before today
} else {
// Date is after today
}
Related
I have a PHP Script which is used to write data to a MYSQL Database.The Script retrieves the data from an Android app and write it to the database table.What I want is : the PHP script will calculate the count of data in the database table according to the date of entry and pass the count to the Android device according to which I need to do some validations in the Android App.Is it possible to do so in the same script? I mean is it possible to write data to a database table from an Android app on a button click and also read the data from the same script into the App.Can anyone please help me with this?
My PHP Script is:
<?php
require "conn.php";
require "SalesLogin.php";
$enquiry = $_POST["enquiry"];
$retail = $_POST["retail"];
$collection = $_POST["collection"];
$booking = $_POST["booking"];
$evaluation = $_POST["evaluation"];
$test_drive = $_POST["test_drive"];
$home_visit = $_POST["home_visit"];
$user_name = $_POST["user_name"];
$update_date = $_POST["date"];
$absent = $_POST["absent"];
$timezone = new DateTimeZone("Asia/Kolkata" );
$date = new DateTime();
$date->setTimezone($timezone );
$time = $date->format( 'H:i:s A' );
$sql = "UPDATE employee_details SET
enquiry_sum = (SELECT SUM(enquiry) +'$enquiry' FROM (SELECT * FROM employee_details WHERE date = CURDATE() AND name = '$user_name') AS x)
WHERE date = CURDATE() AND name = '$user_name'";
$res = $conn->query($sql);
$check = "UPDATE employee_details SET enquiry_target_status = ( SELECT IF (MAX(enquiry_sum) = 52, 'ACHIEVED', 'NOT ACHIEVED') FROM (SELECT * from employee_details WHERE date = CURDATE() AND name = '$user_name') AS Y ) WHERE date = CURDATE() AND name = '$user_name'";
$insert_status = $conn->query($check);
$miss_count = "UPDATE employee_details SET enquiry_target_missed_by = (SELECT (50 - MAX(enquiry_sum)) FROM (SELECT * from employee_details WHERE date = CURDATE() AND name = '$user_name') AS Z ) WHERE date = CURDATE() AND name = '$user_name'";
$insert_status = $conn->query($miss_count);
$mysql_qry1 = "INSERT INTO employee_details(enquiry,retail,
collection,booking, evaluation, test_drive, home_visit, name, date,time,absent) values ('$enquiry','$retail','$collection','$booking','$evaluation','$test_drive',
'$home_visit','$user_name','$update_date','$time','$absent');";
if($conn->query($mysql_qry1) === TRUE)
echo "Your details has been successfully inserted.";
else
echo "Error: " .$mysql_qry1. "<br>" . $conn->error;
if($update_date != $date){
$mysql_qry2 = "UPDATE employee_data SET last_updated_date = (DATE_ADD('$update_date', INTERVAL 1 DAY)) WHERE name = '$user_name';";
$conn->query($mysql_qry2);
echo "Date changed," .$mysql_qry2;
}
$mysql_qry3 = "SELECT COUNT(*) from employee_details WHERE date = '$update_date' and name LIKE '$user_name';";
$conn->query($mysql_qry3);
if($mysl_qry3 <= 2)
{
echo "You can login.";
}
else
{
echo "You cannot login anymore for today.";
}
$conn->close();
?>
I want to check the condition and pass the result for the below part and make validations in Android App:
$mysql_qry3 = "SELECT COUNT(*) from employee_details WHERE date = '$update_date' and name LIKE '$user_name';";
$conn->query($mysql_qry3);
if($mysl_qry3 <= 2)
{
echo "You can login.";
}
else
{
echo "You cannot login anymore for today.";
}
The problem I was making is : I did not use "AS" keyword in the PHP Script.
The updated PHP Script is:
$mysql_qry3 = "SELECT COUNT(*) AS count from employee_details WHERE date = '$update_date' and name LIKE '$user_name';";
$result1 = mysqli_query($conn,$mysql_qry3);
$row = mysqli_fetch_assoc($result1);
$count = $row['count'];
echo "Count: " .$count;
if($count <= 2)
{
echo "You can login.";
}
else
{
echo "You cannot login anymore for today.";
}
This gives the desired result.
I'm working on a event manager and I would like to create a notification system for a group of users.
When an admin creates a new event then php create a notification inside mysql with status 0 if not read and then it changes to status 1 if read.
The problem is that I'm using the group id instead of the user id, and when the first logged user click on the notification it updates the notification status so other users are not viewing the notification. Maybe inserting a row for each user into the database can solve this problem? do I need to use an array of user's ids?
This is what i've done so far:
session_start();
// admin id
$userid = $_SESSION['user_id'];
include('../../../config/database.php');
$event_title = $_POST['titleevent'];
$event_color = $_POST['eventcolor'];
$event_start = $_POST['startevent'];
$new_start_date = date('Y-m-d 00:00:00', strtotime($event_start));
$event_end = $_POST['endevent'];
$new_end_date = date('Y-m-d 23:59:00', strtotime($event_end));
$event_group = $_POST['usergroup'];
$event_description = $_POST['eventdescription'];
if ($create_event = mysqli_prepare($conn, "INSERT INTO user_events (event_title, event_description, event_start, event_end, event_color, event_group)
VALUES (?, ?, ?, ?, ?, ?)" )) {
mysqli_stmt_bind_param($create_event, 'sssssi', $event_title, $event_description, $new_start_date, $new_end_date, $event_color, $event_group);
mysqli_stmt_execute($create_event);
// get event id
$event_id = mysqli_insert_id($conn);
$event_start_date = date('d-m-Y', strtotime($new_start_date));
$notification = "Nuovo evento <span class='text-warning'><strong>$event_title</strong></span> inizia il $event_start_date";
$notification_status = "0";
$notification_category= "events";
$event_notification = mysqli_prepare($conn, "INSERT INTO event_notifications (e_notification_sent_by, e_notification_sent_to, e_notification_message, e_notification_time, e_notification_status, e_notification_category, e_notification_category_id) VALUES(?,?,?,now(),?,?,?)");
mysqli_stmt_bind_param($event_notification, 'iisisi', $userid, $event_group, $notification, $notification_status, $notification_category, $event_id);
mysqli_stmt_execute($event_notification);
mysqli_stmt_close($event_notification);
echo "event created";
mysqli_stmt_close($create_event);
}else{
echo "Ops, error";
}
Many thanks for you help
UPDATE i've managed to make it working using an array and foreach in this way:
$event_start_date = date('d-m-Y', strtotime($new_start_date));
$notification = "Nuovo evento <span class='text-warning'><strong>$event_title</strong></span> inizia il $event_start_date";
$notification_status = "0";
$notification_category= "events";
$sql = "SELECT user_join_id FROM user_group_join WHERE group_join_id='$event_group'";
$result= mysqli_query($conn,$sql);
$datas= array();
if(mysqli_num_rows($result) > 0){
while($row=mysqli_fetch_array($result, MYSQLI_ASSOC)){
$datas[]= $row;
}
}
foreach($datas as $data) {
$id_cliente = $data['user_join_id'];
$event_notification = mysqli_prepare($conn, "INSERT INTO user_notifications (notification_sent_by, notification_sent_to, notification_message, notification_time, notification_status, notification_category, notification_category_id) VALUES(?,?,?,now(),?,?,?)");
mysqli_stmt_bind_param($event_notification, 'iisisi', $userid, $id_cliente, $notification, $notification_status, $notification_category, $event_id);
mysqli_stmt_execute($event_notification);
mysqli_stmt_close($event_notification);
}
Now the question is how can i can i convert it with a prepared statement? And also it looks like i'm going to use a lot of server resources querying the database for each user id, is there any way to optimize the foreach loop?
Many thanks
I have a booking form where the users can book a certain facility, on a certain date and between two times. I have come to a point where my PHP code will prevent the user from booking the same pitch on the same date and from the same start or end time.
However the user can go between a time and book the same facility on the same date as another user. e.g. if one user has booked from 9 to 12 on the 1st May another user can come in and book the 10 or 11 slot.
Is there any way of preventing the user from booking a time between the time another user has already booked???
<?php
include "config.php";
//Booking point
if(isset($_POST['booking']))
{
//get values for variables
$pitchID = $_POST['pitchID'];
$start_date = $_POST['start_date'];
$start_hour = $_POST['start_hour'];
$end_hour = $_POST['end_hour'];
$booking_age = $_POST['booking_age'];
$pitch_size = $_POST['pitch_size'];
$light_tokens = $_POST['light_tokens'];
/* $q = $db->prepare("SELECT * FROM booking WHERE start_date = ?, start_hour = ?, end_hour=?, pitchID=?");
$query = $q->execute(array($start_date, $start_hour, $end_hour, $pitchID));
$count = $q->rowCount(); */
$q = $db->prepare("SELECT * FROM booking WHERE start_date = ? AND start_hour = ? AND pitchID = ?");
$query = $q->execute(array($start_date, $start_hour, $pitchID));
$count = $q->rowCount();
if($count == 0){
$query = $db->prepare("INSERT INTO booking SET pitchID = ?, start_date = ?, start_hour = ?, end_hour = ?, booking_age = ?, pitch_size = ?, light_tokens = ?, userID='$userID'");
$query = $query->execute(array($pitchID,$start_date,$start_hour,$end_hour,$booking_age,$pitch_size,$light_tokens));
if($query){
echo "Your booking has been made";
header("Location:home2_template.html");
return;
} else {
echo "Fail";
} //else fail
} else {
echo "This booking already exists";
} //else count
} //if booking
?>
If you only can reserve one day, try this
select * from booking
where (start_date='new_date'
and (
(start_hour <='new_start_hour' and end_hour>='new_start_hour')
or
(start_hour<= 'new_end_hour' and end_hour>='new_end_hour')
)
) and pitchID = ?
So I am trying to compare today's date and the date of the last entry in the database. For some reason the code isn't working, and the comparison is always wrong.
$recentMood = mysqli_query($db_connection, "SELECT * FROM DemoUser ORDER BY date DESC LIMIT 1'");
$today = date('Y-m-d');
if($recentMood['date'] != $today){
mysqli_query($db_connection, "INSERT INTO DemoUser (Mood, date) VALUES('$mood' , NOW())");
echo "Mood recorded!";
}
else{
echo "Mood already recorded.";
}
P.S: When I try to echo either $today or $recentMood['date'], then I get an error. Any reason why that is?
You are comparing inside the if a resource to a date string.
if($recentMood['date'] != $today){
Fetch the results first:
$query = mysqli_query($db_connection, "SELECT * FROM DemoUser ORDER BY date DESC LIMIT 1");
if($query->num_rows > 0) {
$recentMood = $query->fetch_assoc(); // fetch it, you skipped this step
$today = date('Y-m-d');
$mood = $db_connection->real_escape_string($mood);
if($recentMood['date'] != $today){
mysqli_query($db_connection, "INSERT INTO DemoUser (Mood, date) VALUES('$mood' , NOW())");
echo "Mood recorded!";
} else {
echo "Mood already recorded.";
}
}
I believe that to make this work properly, you need to convert the date from the database into a usable timestamp.
if ($recentMood['date']) {
$past_mood = date('Y-m-d', strtotime($recentMood['date']));
}
$today = date('Y-m-d');
if($past_mood != $today){
mysqli_query($db_connection, "INSERT INTO DemoUser (Mood, date)
VALUES('$mood' , NOW())");
echo "Mood recorded!";
}
This question already has answers here:
Best way to INSERT many values in mysqli?
(4 answers)
Closed 1 year ago.
I'm struggling to revise the following (which works) to use a prepared statement:
echo "<div class=\"debug\">
<h4>values \$_POSTed from *LINE-ITEMS TABLE* in input.php:</h4>
<table>";
foreach ($_POST['date'] as $i => $value) {
$invNum = $_POST['invNum'];
$date = $_POST['date'][$i];
$hours = $_POST['hours'][$i];
$rate = $_POST['rate'][$i];
$dateTotal = $_POST['dateTotal'][$i];
echo "<tr>
<td>".$i."</td>
<td>".$date."</td>
<td>".$hours."</td>
<td>".$rate."</td>
<td>".$dateTotal."</td>
</tr>";
$query = "INSERT INTO Invoice_Line_Items SET
INVOICE_NUMBER = '$invNum',
DATE = '$date',
HOURS = '$hours',
RATE = '$rate',
DATE_TOTAL = '$dateTotal'
ON DUPLICATE KEY UPDATE
INVOICE_NUMBER = VALUES(INVOICE_NUMBER),
DATE = VALUES(DATE),
HOURS = VALUES(HOURS),
RATE = VALUES(RATE),
DATE_TOTAL = VALUES(DATE_TOTAL)
";
} // END foreach
echo "</table></div>";
I've been trying to adapt the (working) prepared statement/query running above this in the same page, which inserts a single row into a different table. But this 2nd query (into a different db table) inserts data from multiple (dynamic # of) rows from a line-items table within the source form.
I've been hacking at it for hours but I can't quite sort out how to implement a prepared statement with the line-items loop. I thought it would be along these lines, but this is not inserting.
echo "<div class=\"debug\">
<h4>values \$_POSTed from *LINE-ITEMS TABLE* in input.php:</h4>
<table>";
// this is the line-items table in the form; don't I have to get these values before the query?
foreach ($_POST['date'] as $i => $value) {
$invNum = $_POST['invNum'];
$date = $_POST['date'][$i];
$hours = $_POST['hours'][$i];
$rate = $_POST['rate'][$i];
$dateTotal = $_POST['dateTotal'][$i];
// confirm vars/values
echo "<tr><td>".$i."</td><td>".$date."</td><td>".$hours."</td><td>".$rate."</td><td>".$dateTotal."</td></tr>";
$stmt = $mysqli->stmt_init();
$query = "INSERT INTO Invoice_Line_Items
INVOICE_NUMBER = '$invNum',
DATE = '$date',
HOURS = '$hours',
RATE = '$rate',
DATE_TOTAL = '$dateTotal'
ON DUPLICATE KEY UPDATE
INVOICE_NUMBER = VALUES(INVOICE_NUMBER),
DATE = VALUES(DATE),
HOURS = VALUES(HOURS),
RATE = VALUES(RATE),
DATE_TOTAL = VALUES(DATE_TOTAL)
";
if ($stmt->prepare($query)) {
$stmt -> bind_param("ssddd", $invNum, $date, $hours, $rate, $dateTotal);
$stmt -> execute();
$stmt->close();
} // if $stmt
} // END foreach
echo "</table></div>";
Can someone please shed some light? Much appreciated.
svs
You don't need
$stmt = $mysqli->stmt_init();
You should be able to just call
$stmt = $mysqli->prepare($query);
Another problem is you're setting the query up inside your loop. You shouldn't do that. Move $stmt outside the loop and only run execute inside once you've set your variables up. Finally, you need to add ? so MySQL knows the parameters
$query = "INSERT INTO Invoice_Line_Items
INVOICE_NUMBER = ?,
DATE = ?,
HOURS = ?,
RATE = ?,
DATE_TOTAL = ?
ON DUPLICATE KEY UPDATE
INVOICE_NUMBER = VALUES(INVOICE_NUMBER),
DATE = VALUES(DATE),
HOURS = VALUES(HOURS),
RATE = VALUES(RATE),
DATE_TOTAL = VALUES(DATE_TOTAL)
";
$invNum = $date = $hours = $rate = $dateTotal = '';
$stmt = $mysqli->prepare($query);
$stmt->bind_param("ssddd", $invNum, $date, $hours, $rate, $dateTotal);
foreach ($_POST['date'] as $i => $value) {
$invNum = $_POST['invNum'];
$date = $_POST['date'][$i];
$hours = $_POST['hours'][$i];
$rate = $_POST['rate'][$i];
$dateTotal = $_POST['dateTotal'][$i];
$stmt->execute();
}