gpt4 book ai didi

php - 防止日期和时间插入数据库

转载 作者:行者123 更新时间:2023-11-29 02:43:44 25 4
gpt4 key购买 nike

我正在尝试创建预订系统,并且我已经创建了包含列(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号房。

提前致谢。

最佳答案

用请求的房间号和新的预订时间从reservations表中计数,并查看表中是否存在从重新开始到重新发送时间之间的记录。

enter image description here

如果我正确理解了您的问题,那么以下两个查询中的任何一个都应该有效。

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;

关于php - 防止日期和时间插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46261613/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com