2015-04-06 81 views
0

我有一個包含客房
的可用性表看起來是這樣的(遺憾的片段)MySQL的&笨更新多行

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
 

 
<html> 
 
    <head> 
 
    <title>availability</title> 
 
    <meta name="GENERATOR" content="HeidiSQL 9.1.0.4936"> 
 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
 
    <style type="text/css"> 
 
     thead tr {background-color: ActiveCaption; color: CaptionText;} 
 
     th, td {vertical-align: top; font-family: "Tahoma", Arial, Helvetica, sans-serif; font-size: 8pt; padding: 3px; } 
 
     table, td {border: 1px solid silver;} 
 
     table {border-collapse: collapse;} 
 
     thead .col0 {width: 106px;} 
 
     .col0 {text-align: right;} 
 
     thead .col1 {width: 80px;} 
 
     .col1 {text-align: right;} 
 
     thead .col2 {width: 79px;} 
 
     .col2 {text-align: right;} 
 
     thead .col3 {width: 80px;} 
 
     .col3 {text-align: right;} 
 
     thead .col4 {width: 91px;} 
 
     thead .col5 {width: 51px;} 
 
     .col5 {text-align: right;} 
 
     thead .col6 {width: 113px;} 
 
     .col6 {text-align: right;} 
 
     thead .col7 {width: 113px;} 
 
     .col7 {text-align: right;} 
 
     thead .col8 {width: 78px;} 
 
     .col8 {text-align: right;} 
 
     thead .col9 {width: 79px;} 
 
     .col9 {text-align: right;} 
 
    </style> 
 
    </head> 
 

 
    <body> 
 

 
<p style="font-family: monospace; white-space: pre;">SELECT * FROM `availability` WHERE today between '2015-04-10' and '2015-04-18' 
 
and idroom = 28 LIMIT 1000</p> 
 

 
    <table caption="jos_bookitavailability (9 rows)"> 
 
     <thead> 
 
     <tr> 
 
      <th class="col0">idavailability</th> 
 
      <th class="col1">idoffer</th> 
 
      <th class="col2">idbook</th> 
 
      <th class="col3">idroom</th> 
 
      <th class="col4">today</th> 
 
      <th class="col5">price</th> 
 
      <th class="col6">price_deviation_1</th> 
 
      <th class="col7">price_deviation_2</th> 
 
      <th class="col8">availability</th> 
 
      <th class="col9">idcategory</th> 
 
     </tr> 
 
     </thead> 
 
     <tbody> 
 
     <tr> 
 
      <td class="col0">149631</td> 
 
      <td class="col1">0</td> 
 
      <td class="col2">0</td> 
 
      <td class="col3">28</td> 
 
      <td class="col4">2015-04-18</td> 
 
      <td class="col5">0</td> 
 
      <td class="col6"></td> 
 
      <td class="col7"></td> 
 
      <td class="col8">0</td> 
 
      <td class="col9">1</td> 
 
     </tr> 
 
     <tr> 
 
      <td class="col0">149630</td> 
 
      <td class="col1">0</td> 
 
      <td class="col2">0</td> 
 
      <td class="col3">28</td> 
 
      <td class="col4">2015-04-17</td> 
 
      <td class="col5">0</td> 
 
      <td class="col6"></td> 
 
      <td class="col7"></td> 
 
      <td class="col8">0</td> 
 
      <td class="col9">1</td> 
 
     </tr> 
 
     <tr> 
 
      <td class="col0">149629</td> 
 
      <td class="col1">0</td> 
 
      <td class="col2">0</td> 
 
      <td class="col3">28</td> 
 
      <td class="col4">2015-04-16</td> 
 
      <td class="col5">0</td> 
 
      <td class="col6"></td> 
 
      <td class="col7"></td> 
 
      <td class="col8">0</td> 
 
      <td class="col9">1</td> 
 
     </tr> 
 
     <tr> 
 
      <td class="col0">149628</td> 
 
      <td class="col1">0</td> 
 
      <td class="col2">0</td> 
 
      <td class="col3">28</td> 
 
      <td class="col4">2015-04-15</td> 
 
      <td class="col5">0</td> 
 
      <td class="col6"></td> 
 
      <td class="col7"></td> 
 
      <td class="col8">0</td> 
 
      <td class="col9">1</td> 
 
     </tr> 
 
     <tr> 
 
      <td class="col0">149627</td> 
 
      <td class="col1">0</td> 
 
      <td class="col2">0</td> 
 
      <td class="col3">28</td> 
 
      <td class="col4">2015-04-14</td> 
 
      <td class="col5">0</td> 
 
      <td class="col6"></td> 
 
      <td class="col7"></td> 
 
      <td class="col8">0</td> 
 
      <td class="col9">1</td> 
 
     </tr> 
 
     <tr> 
 
      <td class="col0">149626</td> 
 
      <td class="col1">0</td> 
 
      <td class="col2">0</td> 
 
      <td class="col3">28</td> 
 
      <td class="col4">2015-04-13</td> 
 
      <td class="col5">0</td> 
 
      <td class="col6"></td> 
 
      <td class="col7"></td> 
 
      <td class="col8">0</td> 
 
      <td class="col9">1</td> 
 
     </tr> 
 
     <tr> 
 
      <td class="col0">149625</td> 
 
      <td class="col1">0</td> 
 
      <td class="col2">0</td> 
 
      <td class="col3">28</td> 
 
      <td class="col4">2015-04-12</td> 
 
      <td class="col5">0</td> 
 
      <td class="col6"></td> 
 
      <td class="col7"></td> 
 
      <td class="col8">0</td> 
 
      <td class="col9">1</td> 
 
     </tr> 
 
     <tr> 
 
      <td class="col0">149624</td> 
 
      <td class="col1">0</td> 
 
      <td class="col2">0</td> 
 
      <td class="col3">28</td> 
 
      <td class="col4">2015-04-11</td> 
 
      <td class="col5">0</td> 
 
      <td class="col6"></td> 
 
      <td class="col7"></td> 
 
      <td class="col8">0</td> 
 
      <td class="col9">1</td> 
 
     </tr> 
 
     <tr> 
 
      <td class="col0">149623</td> 
 
      <td class="col1">0</td> 
 
      <td class="col2">0</td> 
 
      <td class="col3">28</td> 
 
      <td class="col4">2015-04-10</td> 
 
      <td class="col5">0</td> 
 
      <td class="col6"></td> 
 
      <td class="col7"></td> 
 
      <td class="col8">0</td> 
 
      <td class="col9">1</td> 
 
     </tr> 
 
     </tbody> 
 
    </table> 
 

 
    <p> 
 
     <em>generated 2015-04-06 13:09:39  by <a href="http://www.heidisql.com/">HeidiSQL 9.1.0.4936</a></em> 
 
    </p> 
 

 
    </body> 
 
</html>

我想要做什麼表是:當我有新的預訂時,將字段'idbook'更改爲預訂ID,每行的可用性爲1,具體取決於該書的日期(請參見:今日字段)和idroom。
在模型中我有這樣的代碼(介意「調試」)

 
function updateAvailability($data) 
    {

$this->db->select('idbook'); 
    $this->db->where('idguests',$data['guestid']); 
    $this->db->where('valid_from',$data['from_date']); 
    $this->db->where('valid_to',$data['to_date']); 
    $this->db->where('idroom',$data['idroom']); 
    $query = $this->db->get('bookitbooking'); 

    $result = $query->row(); 
    $idbook = $result->idbook; 

    $changes = array(
     'idbook' => $idbook, 
     'availability' => 1 
    ); 
    $where = array(
     'today' => "BETWEEN '{$data['from_date']}' AND '{$data['to_date']}'", 
     'idroom' => $data['idroom'] 
    ); 

    $update = $this->db->update('bookitavailability',$changes,$where); 
    if($update === TRUE){ 
     echo "done"; 
    } else { 
     echo "wrong"; 
    } 

} 

是有原因的,上面的代碼不工作,我有一個「完成」迴歸呼應,但在我的數據庫沒有變化。

+0

請檢查 「BETWEEN '{$數據[' FROM_DATE ']}' 和 '{$數據[' TO_DATE ']}'」,你在雙引號使用多個單引號.... – 2015-04-06 10:24:14

+1

有你嘗試打印查詢? – Raja 2015-04-06 10:26:44

+0

是的,我剛剛做了,它產生了一個錯誤:[代碼]更新'可用性'SET' idbook' ='539','availability' = 1 WHERE'today' ='BETWEEN 2015-04-10和2015-04 -18'和'idroom' ='28'[/ code]你看到where語句是worng,給**今天= ... ** – tsompanis 2015-04-06 11:58:16

回答

0

好... ...因爲我看到有一個問題,與我想要的,希望where子句不能正常工作,我做了一些修改我的代碼。
首先,我爲日期做了foreach()循環,然後我在每個循環中傳遞了我想要的的日期,其中聲明。這是我如何「解決」它。


function updateAvailability($data) 
    { 
     $this->db->select('idbook'); 
     $this->db->where('idguests',$data['guestid']); 
     $this->db->where('valid_from',$data['from_date']); 
     $this->db->where('valid_to',$data['to_date']); 
     $this->db->where('idroom',$data['idroom']); 
     $query = $this->db->get('bookitbooking'); 

     $result = $query->row(); 
     $idbook = $result->idbook; 

     $changes = array(
      'idbook' => $idbook, 
      'availability' => 1 
     ); 

     $arrival = new DateTime($data['from_date']); 
     $departure = new DateTime($data['to_date']); 
     $interval = DateInterval::createFromDateString('1 day'); 
     $period = new DatePeriod($arrival, $interval, $departure); 

     foreach($period as $day){ 
      $this->db->where('idroom',$data['idroom']); 
      $this->db->where('today',$day->format('Y-m-d')); 
      $this->db->update('bookitavailability',$changes); 
     } 
    } 
0

嘗試仔細檢查此字符串:

"BETWEEN '{$data['from_date']}' AND '{$data['to_date']}'" 

"BETWEEN ".$data['from_date']." AND ".$data['to_date'] 
0

問題可能是$數據[ 'FROM_DATE']是一個字符串,所以它可能不會被upadting正確的行。

嘗試

"BETWEEN DATE(".$data['from_date'].") AND DATE(".$data['to_date'] . ")"