我正在尝试制作预订系统,并且我创建了包含列(id,officename,roomname,resstart,resend,resuser)的表保留。重新启动并重新发送是DATATIME
类型。我创建了一个带有日期选择器的表单,并将其成功插入到数据库中。
这是我的PHP文件:
<?php
session_start();
include('includes/config.php');
include('includes/checklogin.php');
check_login();
$username = $_SESSION['username'];
//code for add courses
if($_POST['submit'])
{
$officename=$_POST['officename'];
$roomname=$_POST['roomname'];
$startdate=$_POST['startdate'];
$enddate=$_POST['enddate'];
$query="insert into reservations (officename,roomname,resstart,resend,resuser) values(?,?,?,?,?)";
$stmt = $mysqli->prepare($query);
$stmt->bind_param('sssss',$officename,$roomname,$startdate,$enddate,$username);
if($stmt->execute()){
echo"<script>alert('Your Reservation Has Been Added Successfully');</script>";
}else{
echo"<script>alert('Warning! You cannot Reserve this appointment');</script>";
}
}
?>
<!doctype html>
<html lang="en" class="no-js">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<meta name="theme-color" content="#3e454c">
<title>Make New Reservation</title>
<link rel="stylesheet" href="css/awesome-bootstrap-checkbox.css">
<link rel="stylesheet" href="css/style.css">
<script type="text/javascript" src="js/jquery-1.11.3-jquery.min.js"></script>
<script type="text/javascript" src="js/validation.min.js"></script>
<!--Load Script and Stylesheet -->
<script type="text/javascript" src="jquery.simple-dtpicker.js"></script>
<link type="text/css" href="jquery.simple-dtpicker.css" rel="stylesheet" />
$(document).ready(function() {
$( "#date" ).datepicker({ dateFormat: "yy-m-d" });
});
</script>
</head>
<body>
<?php include('includes/header.php');?>
<div class="ts-main-content">
<?php include('includes/sidebar.php');?>
<div class="content-wrapper">
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<h2 class="page-title">Make New Reservation</h2>
<div class="row">
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-heading">Make New Reservation</div>
<?php echo "<h4>You Logged in As: <span>$username</span></h4>";
?>
<div class="panel-body">
<?php if(isset($_POST['submit']))
{ ?>
<p style="color: red"><?php echo htmlentities($_SESSION['msg']); ?><?php echo htmlentities($_SESSION['msg']=""); ?></p>
<?php } ?>
<form method="post" class="form-horizontal">
<div class="hr-dashed"></div>
<div class="form-group">
<label class="col-sm-2 control-label">Select Office </label>
<div class="col-sm-8">
<Select name="officename" class="form-control" required>
<option value="Select Office">Select Office</option>
<?php
$sql="select * from offices";
$stmt2 = $mysqli->prepare($sql);
//$stmt2->bind_param('i',$roomno);
//$stmt->bind_param('i',$aid);
$stmt2->execute() ;//ok
$res=$stmt2->get_result();
while ($row=$res->fetch_object()) {
echo "<option value=". $row->officename .">" . $row->officename . "</option>";
}
?>
</Select>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Select Room </label>
<div class="col-sm-8">
<Select name="roomname" class="form-control" required>
<option value="Select Room">Select Room</option>
<?php
$sql="select * from rooms";
$stmt2 = $mysqli->prepare($sql);
//$stmt2->bind_param('i',$roomno);
//$stmt->bind_param('i',$aid);
$stmt2->execute() ;//ok
$res=$stmt2->get_result();
while ($row=$res->fetch_object()) {
echo "<option value=". $row->roomname .">" . $row->roomname . "</option>";
}
?>
</Select>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Start time and date</label>
<div class="col-sm-8">
<input type="text" autocomplete="off" name="startdate" value="" required>
<script type="text/javascript">
$(function(){
$('*[name=startdate]').appendDtpicker();
});
</script>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">End time and date</label>
<div class="col-sm-8">
<input type="text" autocomplete="off" name="enddate" value="" required>
<script type="text/javascript">
$(function(){
$('*[name=enddate]').appendDtpicker();
});
</script>
</div>
</div>
<div class="col-sm-8 col-sm-offset-2">
<input class="btn btn-primary" type="submit" name="submit" value="Make New Reservation">
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
如果有用户尝试使用同一房间的已存在时间进行新预订,我希望表单可以防止插入到表中。就像在1:00到2:00保留的房间1,所以下一个用户不能在1:10到2:00注册房间1,但他可以同时注册房间2。
提前致谢。
答案 0 :(得分:1)
从表格预约中获取请求房间号码和新预订时间的计数,并查看表格中是否存在重新开始和重新发送时间之间的记录。
如果我理解你的问题,下面两个查询中的任何一个都应该有效。
select count(1) from reservations where roomname = 'XYZ' and '17-SEP-17' between resstart and resend;
或
select count(1) from reservations where roomname = 'XYZ' and to_date('17-SEP-17') between resstart and resend;
答案 1 :(得分:0)
防止重复的最佳方法是在数据库中创建唯一键。
ALTER TABLE reservations ADD CONSTRAINT constr_reservations UNIQUE (roomname,resstart,resend,resuser)
在这种情况下,您的代码不得更改。 因为你已经抓住了插入失败
if($stmt->execute()){
echo"<script>alert('Your Reservation Has Been Added Successfully');</script>";
}else{
//ROOM DUPLICATE CASE!!!
echo"<script>alert('Warning! You cannot Reserve this appointment');</script>";
}