Php 检查订购日期是否不正确
我有2个功能和一个预订页面 我的检查功能:Php 检查订购日期是否不正确,php,mysqli,pdo,Php,Mysqli,Pdo,我有2个功能和一个预订页面 我的检查功能: $sqlres = 'SELECT * FROM reserveren WHERE ( ? BETWEEN resvan AND restot OR ? BETWEEN resvan AND restot ) AND accomodatie=?'; if($stmt = $con->myconn->prepare($sqlres)){ $stmt->b
$sqlres = 'SELECT * FROM reserveren WHERE (
? BETWEEN resvan AND restot
OR ? BETWEEN resvan AND restot
) AND accomodatie=?';
if($stmt = $con->myconn->prepare($sqlres)){
$stmt->bind_param('ssi', $resvan, $restot,$accomodatie);
$stmt->execute();
$stmt->store_result();
}
if ($stmt->fetch()){
$errmsg[] = "already reserveerd!";
return $errmsg;
}
$sql = 'INSERT INTO reserveren (accomodatie,land_id,personen,userid,resvan,restot,park) VALUES (?,?,?,?,?,?,?)';
if ($stmt = $con->myconn->prepare($sql))
{
$stmt->bind_param('sisisss', $accomodatie,$landid,$personen,$userid,$resvan, $restot,$park);
$stmt->execute();
$stmt->close();
}
else{
die("errormessage: " . $con->myconn->error);
}
我的订单功能:
$sqlres = 'SELECT * FROM reserveren WHERE (
? BETWEEN resvan AND restot
OR ? BETWEEN resvan AND restot
) AND accomodatie=?';
if($stmt = $con->myconn->prepare($sqlres)){
$stmt->bind_param('ssi', $resvan, $restot,$accomodatie);
$stmt->execute();
$stmt->store_result();
}
if ($stmt->fetch()){
$errmsg[] = "already reserveerd!";
return $errmsg;
}
$sql = 'INSERT INTO reserveren (accomodatie,land_id,personen,userid,resvan,restot,park) VALUES (?,?,?,?,?,?,?)';
if ($stmt = $con->myconn->prepare($sql))
{
$stmt->bind_param('sisisss', $accomodatie,$landid,$personen,$userid,$resvan, $restot,$park);
$stmt->execute();
$stmt->close();
}
else{
die("errormessage: " . $con->myconn->error);
}
我的订单页面:
if (isset($_POST['res']) && $_POST['res'] === 'reserveer') {
$personen = $_POST['personen'];
$resvan = $_POST['resvan'];
$restot = $_POST['restot'];
$accomodatie = $_POST['accomodatie'];
$park = $_POST['park'];
$userid = $_SESSION['userid'];
$landid = $_POST['landid'];
$errorMsgs = $user->check($resvan, $restot,$accomodatie);
if (empty($errorMsgs)) {
$user->reserveer($accomodatie,$landid,$personen,$userid,$resvan, $restot,$park);
echo '<div class="alert alert-success" role="alert"><button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">x</span></button>You have succesfully made a reservation.</div>';
exit;
}
foreach ($errorMsgs as $msg) {
echo '<li>'. $msg. '</li>';
}
}
if(isset($\u POST['res'])&&&$\u POST['res']='reserver'){
$personen=$_POST['personen'];
$resvan=$_POST['resvan'];
$restot=$_POST['restot'];
$accomodatie=$_POST['accomodatie'];
$park=$_POST['park'];
$userid=$\会话['userid'];
$landid=$_POST['landid'];
$errorMsgs=$user->check($resvan、$restot、$acomodatie);
if(空($errorMsgs)){
$user->reserver($acomodatie、$landid、$personen、$userid、$resvan、$restot、$park);
echo“您已经成功预订了房间。”;
出口
}
foreach($errorMsgs作为$msg){
回显“”.$msg. ”;
}
}
一切正常,但我的代码中有一个缺陷
第一:
假设有人从2001-01-15到2001-01-15进行了预订,它发出了一条成功的消息,并被预订了。然后有人从2001年1月14日到2001年1月16日进行了预订,这会发出一条成功的消息,同时也会被预订。但是不应该预订,因为15号已经有预订了
第二:
如果从14到16发生相反的情况,那么从15到15,它会给出一条错误消息,说明它已经订购了。所以这是正确的
如果是第一种方式,我如何才能使它发出其已排序的错误消息?您需要将查询更改为:
'SELECT * FROM reserveren WHERE (
? <= restot
AND ? >= resvan
) AND accomodatie=?';
“从reserveren中选择*,其中(
?=resvan
)住宿=?';
我假设第一个参数是开始日期,第二个参数是新预订的结束日期,与restot
(end)和resvan
(start)相同
这将捕获所有重叠的预订。resvan是开始,Resto是结束。@DannyO是的,我的错,这就是我的意思,你不应该使用而不是OR吗?@sagi这是我以前的问题。但这不起作用。它必须在两个日期之间进行检查。这个查询太少了。@DannyO不,不,再试一次,使用或代替,可能重复