验证预约表格

时间:2014-11-26 13:11:19

标签: php mysql database validation

我希望确保客户无法为公司提供的相同服务预订相同的日期/时间。字段的代码如下:

<div class="row">
    <div class="label">Appointment Type:</div><!-- end .label-->
    <div class="input">
    <select input type="text" id="Booking" class="detail" name="ApptType" value="">
    <option value="">Appointment for</option>
    <option value="Consultation">Consultation</option>
    <option value="Photo Shoot">Photo Shoot</option>
    <option value="Dress Fitting">Dress Fitting</option>
    </select>
    </div><!-- end .input -->
    </div><!-- end .row -->

    <div class="row">
    <div class="label">Select Date and Time:</div><!-- end .label-->
    <div class="input">
    <input type="text" id="DateTime" class="detail" name="DateTime" value="">
    </div><!-- end .input-->
    </div><!-- end .row -->

我喜欢规则,在2014年12月10日下午2点(例如)预约咨询(ApptType)的客户,其他客户无法预订相同的日期或时间。我仍然是新手,但我在搜索代码后尝试了这段代码(对于我的database.php):

 $bookingdatetime = $_POST[DateTime];
  $apptdatetime = Appt_DateTime;

$conn = dbConnect();

if (! $conn)

    die("Couldn't connect to MySQL");

    if  ($apptdatetime != $bookingdatetime);

    return false;

} else { 

    return $sql = "INSERT INTO Appointment (Client_ID,Svc_ID,Appt_DateTime) 
VALUES ('$_POST[ClientID]','$_POST[ApptType]','".date('Y-m-d H:i:s', strtotime($_POST[DateTime]))."')";


mysql_query($sql,$conn);

Dreamweaver中没有写错误。有人可以提供帮助,现在我甚至无法在插入规则后将数据存储在我的数据库中。

1 个答案:

答案 0 :(得分:0)

以下是表单HTML:

<div class="row">
<div class="label">Appointment Type:</div><!-- end .label-->
<div class="input">
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
    <select input type="text" id="Booking" class="detail" name="ApptType" value="">
    <option value="">Appointment for</option>
    <option value="Consultation">Consultation</option>
    <option value="Photo Shoot">Photo Shoot</option>
    <option value="Dress Fitting">Dress Fitting</option>
    </select>
    </div><!-- end .input -->

    <div class="label">Select Date and Time:</div><!-- end .label-->
    <div class="input">
    <input type="text" id="DateTime" class="detail" name="DateTime" value="">
        <input type="submit" name="submit" value="submit">
</form>
</div><!-- end .input-->
</div><!-- end .row -->

以下是PDO代码:

$host = 'localhost';
$dbname = 'xxx';
$user = 'xxx';
$pass = 'xxx';

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

    $DateTime = $_POST['DateTime'];
    $DateTimeCheck = date("Y-m-d H:00:00",strtotime($DateTime));
    $ApptType = $_POST['ApptType'];

        $conn = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);  
        $conn->exec("SET CHARACTER SET utf8");

        $sql = "SELECT DateTime FROM appointments WHERE `DateTime` = :DateTime";
        $sqlprep = $conn->prepare($sql); 

        $ar_val = array(':DateTime'=>$DateTimeCheck);

        if($sqlprep->execute($ar_val)) {
            while($row = $sqlprep->fetch(PDO::FETCH_OBJ)){

                $DateTimeExists = $row->DateTime;
            }
        }

    if(isset($DateTimeExists) && $DateTimeExists!=''){

            echo "Sorry that time slot is already taken";

    } else {

           $conn = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
           $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

       $sql = "INSERT INTO appointments ( DateTime, ApptType ) VALUES ( :DateTime, :ApptType )";

           $q = $conn->prepare($sql);
           $q->execute(array(':DateTime'=>$DateTime,
             ':ApptType'=>$ApptType));

           echo "Your appointment has been created.";

           $conn = null;

    }



}

您需要一些方法将时间段舍入到小时或半小时或其他任何方法。在表单的下拉菜单中执行此操作。否则,可以预订以下时间段:2014-10-10 09:09:00&amp; 2014-10-10 09:09:01 I.e. 1秒后的时隙是有效时隙。

我补充说:$ DateTimeCheck这会将时段缩短到小时并检查相同的小时时段,因此避免上述一秒间隔的问题被预订为单独的时段。