2017-10-07 24 views
0

我想阻止用戶輸入已經採取的日期和數據庫內。就像他們輸入日期類似於數據庫中的日期一樣,他們會收到一條警告,提示「您選擇的日期已經被使用」。我有這樣的代碼,但它沒有這樣做,陷阱在php採取的日期

DATABASE

CREATE TABLE `date_tbl` (
    `date_id` int(11) NOT NULL, 
    `date` date NOT NULL, 
    `time` text NOT NULL, 
    `ordertype` int(10) NOT NULL 
) ENGINE=InnoDB DEFAULT CHARSET=latin1; 

here

PHP

<?php 
     include('db.php'); 
     if(isset($_POST['reserve'])){ 
      $fdate = $_POST['datepicker']; 
      $ftime = $_POST['time']; 

      $fdate_array = array(); 
      $ftime_array = array(); 

      $q=$conn->query("SELECT * FROM date_tbl"); 

      while($row=mysqli_fetch_array($q)){ 
      $fdate_array[]=$row['date']; 
      $ftime_array[]=$row['time']; 
      } 
      if(array_intersect($fdate_array, $ftime_array)){ 
       die ("<script> 
        alert('Date is not available! Please pick another date'); 
        window.location.href='index.php'; 
        </script>"); 
      }else{ 
       $insertDate=$conn->query("INSERT INTO date_tbl (date_id, date, time, ordertype, client_id) VALUES ('','$fdate','$ftime',0)"); 
       if(!$insertDate){ 
         die('error'); 
       }else{ 
         $setin = $conn->query('SET foreign_key_checks = 1'); 
         die("<script> 
          alert('You have reserved a date.'); 
          window.location.href='index.php'; 
          </script>"); 
       } 
      } 
    } 

HTML

<div class="col-md-4"> 
 
    <h5 style="font-family:Lucida Calligraphy">Date:</h5> 
 
    <input type="text" name="datepicker" class="form-control" placeholder='Select date' id="datepicker" style="margin-left: -15px" /> 
 
    <script> 
 
    $(function() { 
 
     $('#datepicker').datepicker({ 
 
     minDate: 0, 
 
     dateFormat: 'yy-mm-dd' 
 
     }); 
 
    }); 
 
    </script> 
 
</div> 
 

 
<div class="col-md-4"> 
 
    <h5 style="font-family:Lucida Calligraphy">Time:</h5> 
 
    <select type="text" name="time" id="time" class="form-control"> 
 
      <option>Select time</option> 
 
      <option>10:00 AM</option> 
 
      <option>1:00 PM</option> 
 
     </select> 
 
</div>

+1

什麼是'datepicker','time'和'date_tbl'相當於字段的格式? – Optimae

+0

@Optimae我現在編輯它我也添加了我的數據庫。 –

+0

您可以將'date_tbl'字段設置爲唯一,並捕獲與違反約束條件有關的任何插入錯誤。你所擁有的問題是,如果兩個請求在同一時間採用完全相同的日期,他們都可能成功。 – apokryfos

回答

0

我覺得你在你的代碼有錯誤

讓我們來看看你的代碼:

$fdate = $_POST['datepicker']; 
$ftime = $_POST['time']; 

首先你存儲這兩個值到瓦爾

那麼你把所有的日期和時間放入數組中。

$fdate_array = array(); 
$ftime_array = array(); 

$q=$conn->query("SELECT * FROM date_tbl"); 
// i assume this works 
while($row=mysqli_fetch_array($q)){ 
    $fdate_array[]=$row['date']; 
    $ftime_array[]=$row['time']; 
} 

...現在的檢查:

if(array_intersect($fdate_array, $ftime_array)){ 

,在這裏你看,如果日期和時間相交...這希望他們不會(因爲日期通常不匹配一次)。

,如果你只是想檢查的日期,這很簡單:

if(in_array($fdate, $fdate_array)) { 
    die('error message'); 
} 

,如果你想查詢的日期時間組合,這是一個有點困難:

for($i = 0; $i < count($fdate_array); $i++) { 
    if($fdate_array[$i] == $fdate && $ftime_array[$i] == $ftime) { 
     die('error message'); 
    } 
} 

然而,這是非常不方便的。乾淨的解決辦法是這樣的:

// assuming pdo here, if you use mysqli, check how it's done there 
$stmt = $conn->prepare('SELECT COUNT(*) FROM date_tbl WHERE date=:date AND time=:time'); 
$stmt->bindValue(':date', $fdate); 
$stmt->bindValue(':time', $ftime); 
$stmt->execute(); 
if($stmt->fetchColumn()) { 
    die('error message'); 
} 

這將使用數據庫來尋找衝突的日期 - 時間組合(這是更有效)。使用預準備語句,只需獲取計數,fetchColumn就可以獲取計數。祝你好運。

+0

'致命的錯誤:未捕獲的錯誤:調用成員函數bindValue()在布爾'我得到這個錯誤 –

+0

所以你不使用pdo,是否正確?爲什麼你會忽略其餘的?對此有何評論? – Jakumi

+0

這一切都很好,我只是使用僅限日期的條件,因爲它現在是我唯一的工作。感謝您的時間 –

0

date_tbl的數據庫設計有一個主要問題。 time列存儲爲文本字段。由於日期和時間一起用於識別特定時間點,所以將它們存儲在類型爲datetime的列中會更有效率。 (MySQL也有time類型,但更適合於持續時間。)

Datetime也允許您正確地對項目進行排序。畢竟,您希望8 AM1 PM之前出現。

爲了簡化代碼,您需要以24h格式發佈時間值。當然,您的用戶仍然想使用12小時格式來選擇,但是這是沒有問題的:

<select type="text" name="time" id="time" class="form-control"> 
    <option value="">Select time</option> 
    <option value="10:00">10:00 AM</option> 
    <option value="13:00">1:00 PM</option> 
</select> 

然後簡單的$fdate$ftime值相結合,$fdatetime

$fdatetime = $fdate . ' ' . $ftime; 

然後,您可以使用$fdatetime在查詢中的價值(並使用像Jakumi建議讓數據庫完成所有工作的查詢)。

顯示日期和時間,結合功能strtotime()date()