[英]mysql & codeigniter update multiple rows
I have a table containing the availability of rooms 我有一张桌子,上面有房间
the table looks something like this (sorry for the snippet) 桌子看起来像这样(对不起代码段)
<!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>
What i want to do is: when i have a new booking, change the field 'idbook' to the booking id and the availability to 1 for each row depending on dates the book (see: today field) and idroom. 我想做的是:当我有一个新的预订时,将“ idbook”字段更改为预订ID,并将每行的可用性更改为1,具体取决于书籍的日期(请参阅“今天”字段)和idroom。
In the model i have this code (mind the "debugging") 在模型中,我有此代码(注意“调试”)
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";
}
}
For a reason, the above code doesnt work, i have a "done" return echoed, but no changes in my DB.
Try to double check this string: 尝试仔细检查以下字符串:
"BETWEEN '{$data['from_date']}' AND '{$data['to_date']}'"
to 至
"BETWEEN ".$data['from_date']." AND ".$data['to_date']
The Problem may be $data['from_date'] is a string so it may not be upadting the correct row. 问题可能是$ data ['from_date']是一个字符串,因此它可能不会更新正确的行。
try 尝试
"BETWEEN DATE(".$data['from_date'].") AND DATE(".$data['to_date'] . ")"
Ok... So since i saw there was an issue with the where clause not functioning as i wanted and hoped, i've made some changes to my code. 好的...因此,由于我看到where子句无法按我希望的那样工作,所以我对代码进行了一些更改。
First, i've made a foreach() loop for the dates and then i passed in each loop, the dates i wanted for the where statement. 首先,我为日期创建了一个foreach()循环,然后在每个循环中传递了我想要的where语句日期。 Here's how i "solved" it.
这就是我“解决”它的方法。
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.