我有一个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";
}
请停止使用sprintf的创建SQL查询。使用参数化查询,因为Bobby表。 – idstam 2014-12-05 14:51:34