Php 选择查询以检查SQL数据库中的重叠日期,然后更改状态
我对PHP非常陌生,因此非常感谢您的帮助!我不确定我写的是否正确。我试着将其他论坛的不同部分拼凑起来,形成一个查询,检查数据库中是否有重叠的日期,然后将请求插入数据库,或者回显它们不可用。 我真的不确定是否对$result变量或if语句使用了正确的语法。我也在尝试使用PDO。 我知道与数据库的连接工作正常,因为当前它正在将请求插入数据库并回显消息,即使我选择了重叠的日期 我尝试了几种不同的方法来比较日期,但我似乎无法让它发挥作用,而这是最直接的方法Php 选择查询以检查SQL数据库中的重叠日期,然后更改状态,php,sql,select,Php,Sql,Select,我对PHP非常陌生,因此非常感谢您的帮助!我不确定我写的是否正确。我试着将其他论坛的不同部分拼凑起来,形成一个查询,检查数据库中是否有重叠的日期,然后将请求插入数据库,或者回显它们不可用。 我真的不确定是否对$result变量或if语句使用了正确的语法。我也在尝试使用PDO。 我知道与数据库的连接工作正常,因为当前它正在将请求插入数据库并回显消息,即使我选择了重叠的日期 我尝试了几种不同的方法来比较日期,但我似乎无法让它发挥作用,而这是最直接的方法 <?php if(isset($_PO
<?php
if(isset($_POST['request_date'])) {
$user_id = $_SESSION['user_id'];
$first_name = $_SESSION['first_name'];
$last_name = $_SESSION['last_name'];
// $request_time = date("d-m-Y H:i:s");
$lend_status = 1;
$lend_id = $_POST['lend_id'];
$requested_start_date = $_POST['requested_start_date'];
$requested_end_date = $_POST['requested_end_date'];
$comments = $_POST['comments'];
$STH = $DBH->prepare("SELECT * FROM laptop_system WHERE approved_end_date >= requested_start_date AND requested_end_date >= approved_start_date");
$STH->execute(array());
$result = $STH->setFetchMode(PDO::FETCH_ASSOC);
if($STH->rowCount() > 0){
echo 'sorry this date is not available';
}
else {
$STH = $DBH->prepare("INSERT INTO laptop_system (user_id,first_name,last_name,lend_status,requested_start_date,requested_end_date,comments) VALUES(?,?,?,?,?,?,?)");
$STH->execute(array($user_id,$first_name,$last_name,$lend_status,$requested_start_date,$requested_end_date,$comments));
echo $first_name . ' your laptop request is now pending';
}
}
?>
<form action="" method="post" >
<label for="requested_start_date"> Requested Start Date
<input type="date" name="requested_start_date" value="<?php echo $requested_start_date; ?>">
</label>
<label for="requested_end_date">Requested End Date
<input type="date" name="requested_end_date" value="<?php echo $requested_end_date; ?>">
</label>
<input type="hidden" name="lend_id" value="<?php echo $lend_id; ?>" />
<input type="textarea" rows="4" cols="50" name="comments" placeholder="Please add any specific requirements that you might need the laptop to have and what it is beign used for. Thank you.">
<input type="submit" name="request_date" value="Request Date">
</form>
请求的开始日期
这是因为在执行select语句后,您以错误的方式使用了select查询,因此必须在$result变量中获取结果
$STH = $DBH->prepare("SELECT * FROM laptop_system WHERE approved_end_date >= requested_start_date AND approved_start_date <= requested_end_date");
$STH->execute();
$result = $STH->setFetchMode(PDO::FETCH_ASSOC); // add this line after excute statement
$STH=$DBH->prepare(“从笔记本电脑系统中选择*,其中批准的结束日期>=请求的开始日期和批准的开始日期,供其他人查看同一事物的未来参考-这是一位朋友帮助我的方法
<?php
if (isset($_POST['request_date'])) {
$user_id = $_SESSION['user_id'];
$first_name = $_SESSION['first_name'];
$last_name = $_SESSION['last_name'];
// $request_time = date("d-m-Y H:i:s");
$lend_status = 1;
$lend_id = $_POST['lend_id'];
$requested_start_date = $_POST['requested_start_date'];
$requested_end_date = $_POST['requested_end_date'];
$comments = $_POST['comments'];
// Declare available laptops array
$available_laptops = array();
// GET ALL LAPTOPS IN OPERATION
$STH = $DBH->prepare("
SELECT laptop_id,
laptop_name
FROM laptops
WHERE 1
");
$STH->execute(array());
while ($row = $STH->fetch()) {
// CHECK EACH LAPTOP FOR THE REQUESTED DATES
$STH2 = $DBH->prepare("
SELECT lend_id
FROM laptop_system
WHERE (
(
DATE(approved_start_date) <= ?
AND DATE(approved_end_date) > ?
) OR (
DATE(approved_start_date) < ?
AND DATE(approved_end_date) >= ?
) OR (
DATE(approved_start_date) >= ?
AND DATE(approved_end_date) <= ?
)
)
AND laptop_id = ?
");
$STH2->execute(array(
$requested_start_date,
$requested_start_date,
$requested_end_date,
$requested_end_date,
$requested_start_date,
$requested_end_date,
$row->laptop_id
));
// IF IT'S NOT BOOKED OUT, ADD TO ARRAY
if ($STH2->rowCount() < 1) {
$available_laptops[$row->laptop_id] = $row->laptop_name;
}
}
if (empty($available_laptops)) {
echo 'Sorry, this date is not available.';
} else {
$STH = $DBH->prepare("
INSERT INTO laptop_system (
user_id,
first_name,
last_name,
lend_status,
requested_start_date,
requested_end_date,
comments
)
VALUES(?, ?, ?, ?, ?, ?, ?)
");
$STH->execute(array(
$user_id,
$first_name,
$last_name,
$lend_status,
$requested_start_date,
$requested_end_date,
$comments
));
echo $first_name . ', your laptop request is now pending approval.';
}
} ?>
<form action="" method="post" >
<label for="requested_start_date"> Requested Start Date
<input type="date" name="requested_start_date" value="<?php echo $requested_start_date; ?>">
</label>
<label for="requested_end_date">Requested End Date
<input type="date" name="requested_end_date" value="<?php echo $requested_end_date; ?>">
</label>
<input type="hidden" name="lend_id" value="<?php echo $lend_id; ?>" />
<input type="textarea" rows="4" cols="50" name="comments" placeholder="Please add any specific requirements that you might need the laptop to have and what it is beign used for. Thank you.">
<input type="submit" name="request_date" value="Request Date">
</form>
感谢您的回复,我在中添加了一条消息,但是它现在没有提交到数据库,即使我选择了免费的日期,它也会回显“不可用”消息……您必须使用PDO计数方法更改if条件如果($STH->rowCount()>0),然后插入“抱歉此日期不可用”或“其他”“即使我选择了免费的日期…您在哪里选择这些日期,因为第一个查询没有参数化表单提交的任何值?如果(isset($\u POST['request\u date'])){对于底部表单提交的select查询,应该对此进行操作-我认为….@Parfati可以在您发布的代码中看到该例程。但是,您的第一次查询没有在select
查询中使用?
参数化任何值,因此您如何选择免费的日期?听起来您需要使用相应的$POST
变量在查询中参数化请求的开始日期和请求的结束日期。正如您所发现的,此查询将始终返回多行。我现在已根据下面的注释进行了更新,但仍无法确定选择时是否存在重叠日期,它仍将发布到数据库,而不是而不是回显错误消息。