2014-12-05 57 views
0

我有一个SQL预订问题。该表具有idstylist,bdate,stime和etime。sql得到两次预订冲突

基本上,造型师一次只能做一项工作。因此,在预订时,我需要检查一下设计师是否已经忙于其他预订。这是我的冲突方法;

public function checkClash($bdate, $stime,$stylist){ 
    $query = sprintf("SELECT id FROM table WHERE (date=%s AND idstylist=%s) AND (%s BETWEEN stime AND etime)", 

     $this->db->GetSQLValueString($bdate, "date"), 
     $this->db->GetSQLValueString($stylist, "int"), 
     $this->db->GetSQLValueString($stime, "text")); 
     $result = $this->db->query($query); 
     if($result && $this->db->num_rows($result) > 0){ 
       return true; 

     } 
     return false; 

} 

据我了解我的查询与选择所有预订(日期)其中idstylist是这一点,并开始时间为> = $ STIME但< = $ STIME。

因此,例如,我在数据库中有一行,看起来像这样;

id |日期| idstylist | stime | etime

1 | 2014-12-05 | 68 | 07:00:00 | 07:30:00

现在我试图在同一天07:10:00为同一个设计师预订,但应该会失败,但系统允许我进行预订。我无法看到我的查询有什么问题。

//next check for clashes in the booking table 
    $clash = $bookings->checkClash($_POST['bdate'],$_POST['stime'], $_POST['stylist']); 
    if($clash==true){ 
     $errors[] = "Booking clashes. Please try again"; 
    } 
+0

请停止使用sprintf的创建SQL查询。使用参数化查询,因为Bobby表。 – idstam 2014-12-05 14:51:34

回答

1

这正在变成一个调试会话。

从SQL:(STIME> =%s和ETIME < =%S)

与值替换:( '7:00'> = '7:10' 和 '7:30' < = '7:10')

我客串你想要的:(%S之间STIME和ETIME)

+0

,通过罚款,并允许另一个预订 – 2014-12-05 15:10:18

+0

我改变了我的查询,以配合你下面写的内容,现在它工作正常非常感谢你。我将上面的代码更新为可行的代码 – 2014-12-05 15:40:13