在Codeigniter

时间:2015-07-06 11:04:08

标签: codeigniter

在我的应用程序控制器中,我有一个方法transaction_reimburse(),它接收id为POST字符串值。该方法的功能是获取与数据库中的id匹配的所有事务,并相应地更新它们。

控制器方法:

public function transaction_reimburse() {

    $reimburse = array('reimburse' => 1);
    $transactions = $this->Transaction_model->get_these_ids($this->input->post('ids'));
    $this->Transaction_model->reimburse_these($transactions, $reimburse);

}

模型方法:

function get_these_ids($ids) {

    $this->db->select('tbl_user.name as uname, tbl_transactions.participant_id as pid, tbl_transactions.card_number as card_no, tbl_transactions.group as ugroup, tbl_toilet.name as utoilet, tbl_transactions.amount_paid as u_amount, tbl_transactions.toilet_price as t_price, tbl_transactions.reimburse_amount as r_amount, tbl_transactions.details as u_details');
    $this->db->from('tbl_transactions');
    $this->db->join('tbl_user', 'tbl_user.participant_id = tbl_transactions.participant_id', 'left');
    $this->db->join('tbl_toilet', 'tbl_toilet.toilet_code = tbl_transactions.toilet_code', 'left');
    $this->db->where("tbl_transactions.id IN (". $ids .")");
    return $this->db->get();

}

模型方法:

function reimburse_these($transactions, $reimburse) {

    $this->db->select('tbl_user.name as uname, tbl_transactions.participant_id as pid, tbl_transactions.card_number as card_no, tbl_transactions.group as ugroup, tbl_toilet.name as utoilet, tbl_transactions.amount_paid as u_amount, tbl_transactions.toilet_price as t_price, tbl_transactions.reimburse_amount as r_amount, tbl_transactions.details as u_details');
    $this->db->from('tbl_transactions');
    $this->db->join('tbl_user', 'tbl_user.participant_id = tbl_transactions.participant_id', 'left');
    $this->db->join('tbl_toilet', 'tbl_toilet.toilet_code = tbl_transactions.toilet_code', 'left');
    $this->db->where("tbl_transactions.id IN (". $transactions .")");
    $this->db->update($this->tbl_transaction, $reimburse);

}

但是,我收到以下错误:

A PHP Error was encountered    
Severity: 4096
Message: Object of class CI_DB_mysql_result could not be converted to string
Filename: models/transaction_model.php
Line Number: 450

A Database Error Occurred
Error Number: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1
UPDATE `tbl_transactions` SET `reimburse` = 1 WHERE `tbl_transactions`.`id` IN ()
Filename: C:\xampp\htdocs\ipa\system\database\DB_driver.php
Line Number: 330

我需要知道的是为什么我会收到这些错误?

3 个答案:

答案 0 :(得分:0)

错误意味着您的$ids变量不是字符串格式(意思是:1,2,3,4。它可能是一个数组)。要解决此问题,请先使用print_r($ids)var_dump($ids)查看其格式。一旦知道了格式,就可以使用PHP函数(例如implode())将其转换为字符串。

答案 1 :(得分:0)

IN正在寻找一种格式,例如: (1,2,3,4)

要么使您的帖子值采用格式1,2,3,4,以便当该值包含在查询中时它有效,或者如果您要发送数组,请使用PHP中的方法,例如{ {1}}创建用于绑定到查询的所需格式..

答案 2 :(得分:0)

我已经查看了你们的两个函数,发现在两个函数中你把tbl_transactions.id放在where子句中,而ids是从post传递的,因此不需要调用get_these_ids来获取id,你已经在post数组中有id了因此,不要使用交易变量,只需使用$ this-> input-> post('ids')来表示reimburse_these函数。

如果您确实想从get_these_ids函数获取tbl_transactions,那么您需要在查询get_these_ids函数时使用group_concat和group by,只需在get_these_ids函数中返回该连接的id,然后它就可以工作。