mysql& codeigniter更新多行

时间:2015-04-06 10:19:51

标签: php mysql codeigniter

我有一张包含房间可用性的表格 该表看起来像这样(抱歉代码段)



<!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>
&#13;
&#13;
&#13;

我想要做的是:当我有新的预订时,请更改字段&#39; idbook&#39;取决于书的日期(参见:今日字段)和idroom,每个行的预订ID和可用性为1 在模型中我有这个代码(注意&#34;调试&#34;)

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";
    }

}

出于某种原因,上面的代码不起作用,我有一个&#34;完成&#34;返回回显,但我的数据库没有变化。

3 个答案:

答案 0 :(得分:0)

尝试仔细检查此字符串:

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

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

答案 1 :(得分:0)

问题可能是$ data [&#39; from_date&#39;]是一个字符串,因此它可能没有正确的行。

尝试

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

答案 2 :(得分:0)

好的...所以,因为我看到where子句无法按照我想要和希望运行的问题,我对代码进行了一些更改。
首先,我为日期做了一个foreach()循环,然后我在每个循环中传递了我想要的 where 语句的日期。这是我“解决”它的方式。


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);
        }
    }