简体   繁体   中英

Select query to check for overlapping dates in SQL database and then change status

I'm very new to PHP so any help would be really appreciated! I'm am not sure if I have written this correctly. I've tried to piece together different bits from other forums to form a query that will check if there are overlapping dates from a database and then either insert the request into the database or echo that they aren't available. I'm really not sure if I've used the correct syntax with the $result variable, or the IF statements. I'm also trying to use PDO. I know the connection to the database works as currently it's inserting the request into the database and echoing out the message even though I've selected overlapping dates.

I've tried several different methods to compare the dates but I can't seem to get it to work and this one was the most straight forward method.

<?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>

It's inserting into the database even though I'm selecting the same dates. Any help would be great, thank you!

It because you have used the select query in the wrong way after execute of the select statement you have to fetch the result in the $result variable

$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

You can refer W3school for more understanding of how PDO statement works: https://www.w3schools.com/php/php_mysql_select.asp

For future reference for anyone else looking at the same thing - this was the method that a friend helped me with that works!

    <?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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM