使用数据库中的值检查变量值

时间:2019-11-28 08:05:08

标签: php

我已经尝试了2天,但没有成功。我正在尝试对照数据库“预订”表中名为“日期”的列中的现有值来检查表单输入值。如果找到任何匹配项,则整个结果必须为false,以便查询不会运行。

我的代码如此杂乱无章,但我现在展示的是唯一可以使一半有效的想法。 用户选择日期或日期范围。将对照数据库中“日期”列中的日期检查这些日期。如果存在任何日期,则整个过程都必须为false;如果找不到任何日期,则必须为true,然后可以将这些值保存到数据库中。 这是我所拥有的:

    $sql = "
SELECT date
FROM bookings
;";

$result = $conn->query($sql);
$alreadyBooked = array();

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        $alreadyBooked[] = $row['date'];
    } // while
} //  num rows


if(isset($_POST['submit'])){

$name = $_POST['name'];
$email = $_POST['email'];
$tel = $_POST['tel'];
$arrive = strtotime($_POST['arrive']);
$depart = (strtotime($_POST['depart'])) - 86400;
?>

<p>You have chosen the following dates: </p>
 <a name="booking-form"></a> 

<?php



for ( $i = $arrive; $i <= $depart; $i = $i + 86400 ) {
        echo $bookedDates = date('Y-m-d', $i );


        if (in_array($bookedDates, $alreadyBooked)){
            $bookdedDatesNice = date('D, j M Y', strtotime($bookedDates));
            echo 'This date is not available : '.$bookdedDatesNice.'<br />';

        } else {

            echo 'Your dates are available';

            $sqlinsert = "INSERT INTO bookings (name, email, tel, roomid, date) values ";
            // Loop between timestamps, 24 hours at a time
                for ( $i = $arrive; $i <= $depart; $i = $i + 86400 ) {
                    $bookedDates =date('Y-m-d', $i ); 
                    // Constructing the insert query
                 $sqlinsert .= " ('$name', '$email', '$tel', '$roomId', '$bookedDates'),";
            }

            $bulk_insert_query = rtrim($sqlinsert, ","); // to remove last comma
            $resultinsert = $conn->query($bulk_insert_query); 
            ?>


            <p>Your booking has been received for: <br />
            Please wait for a confirmation email on: <?php echo $email; ?>

            </p>



        <?php
        // header('refresh:3; url=book.php?id='.$id.'#booking-form');

        }// if  
}   // for

}

Please can someone help out by showing me an example code of how to solve this issue. I am still a newbie at PHP.

2 个答案:

答案 0 :(得分:0)

发出一个SQL请求,将响应填充到一个数组中(您已经做过),创建一个将数组作为参数的函数。

function checkDate($dates){ //dates is the array you get from your SQL
   foreach($dates as $date){
     if($_POST['date'] == $date){
     return false;
     }
   }
return true;
}

如果您对该功能的判断是正确的,则可以将该日期输入数据库,如果为假,则您已经预订了该日期。

答案 1 :(得分:0)

我不知道如何将插入代码更改为准备好的语句,因为我在互联网上发现了这种批量插入代码,说实话也不知道如何将其转换为准备好的语句。

我设法弄清楚了:

<?php
            $sql = "
            SELECT date
            FROM bookings
            WHERE roomid = $roomId;
            ;";

            $result = $conn->query($sql);
            $alreadyBooked = array();

            if ($result->num_rows > 0) {
                // output data of each row
                while($row = $result->fetch_assoc()) {
                    $alreadyBooked[] = $row['date'];
                } // while
            } //  num rows






            if(isset($_POST['submit'])){

                $name = $_POST['name'];
                $email = $_POST['email'];
                $tel = $_POST['tel'];
                $arrive = strtotime($_POST['arrive']);
                $depart = (strtotime($_POST['depart'])) - 86400;
                ?>

                <a name="booking-form"></a> 
                <h2>Viewing <?php echo $roomname; ?></h2>
                <h3>You have chosen the following dates: </h3>



                <?php
                // show which dates the user chose
                for ( $i = $arrive; $i <= $depart; $i = $i + 86400 ) {
                        echo $bookedDatesecho = date('D, j M Y', $i ).'<br />';
                 }

                // save the dates the user chose into an array
                for ( $i = $arrive; $i <= $depart; $i = $i + 86400 ) {
                    $bookedDates[] = date('Y-m-d', $i );
                }                   

                // checking dates chosen from form with dates in the database
                $compareDateResult = array_intersect($alreadyBooked,$bookedDates);

                // if statement to check whether the dates chosen are available or not and then do something
                if (empty($compareDateResult)){

                    // save booking to the database
                    $sqlinsert = "INSERT INTO bookings (name, email, tel, roomid, date) values ";
                    // Loop between timestamps, 24 hours at a time
                        for ( $i = $arrive; $i <= $depart; $i = $i + 86400 ) {
                            $bookedDates =date('Y-m-d', $i ); 
                            // Constructing the insert query
                         $sqlinsert .= " ('$name', '$email', '$tel', '$roomId', '$bookedDates'),";
                    }

                    $bulk_insert_query = rtrim($sqlinsert, ","); // to remove last comma
                    $resultinsert = $conn->query($bulk_insert_query);   
                    ?>
                    <p>Your booking has been received for: <br />
                    Please wait for a confirmation email on: <?php echo $email; ?></p>

                <?php 
                } else { ?>

                    <h3>These dates are not available:</h3>


                    <?php 
                    foreach ($compareDateResult as $value) {
                        //echo 'the date: '.$value.' <br />';
                        $value2 = strtotime($value);
                        echo date('D, j M Y',$value2).'<br />'; 
                    } // foreach
                    ?>
                    <p><a href="book.php?id=<?php echo $id; ?>">Please try again</a></p>

                <?php   
                } // if                 




             } else {  ?>



                <h2>Booking Form</h2>       
                <form id="booking-form" method="post" action="#booking-form" />

                    <input type="hidden" name="<?php echo $roomId; ?>" />

                    <label>Name</label><br />
                    <input type="text" name="name" required />
                    <div class="clear20"></div>

                    <label>Email</label><br />
                    <input type="email" name="email" required/>
                    <div class="clear20"></div>

                    <label>Telephone</label><br />
                    <input type="text" name="tel" required />
                    <div class="clear20"></div>

                    <label>Arrive on: </label>
                    <input type="date" name="arrive" required/> at 2pm
                    <div class="clear20"></div>

                    <label>Depart on: </label>
                    <input type="date" name="depart" required/> at noon
                    <div class="clear20"></div>

                    <input type="submit" name="submit" value="Book &raquo;"/>

                </form>

            <?php }     ?>