从Codeigniter中的数据库检索和更新数据时出错

从Codeigniter中的数据库检索和更新数据时出错,codeigniter,Codeigniter,在我的应用程序控制器中,我有一个方法transaction\u represent(),它以POST字符串值的形式接收ID。该方法的功能是从数据库中获取与ID匹配的所有事务,并相应地更新它们 控制器方法: public function transaction_reimburse() { $reimburse = array('reimburse' => 1); $transactions = $this->Transaction_model->get_the

在我的应用程序控制器中,我有一个方法
transaction\u represent()
,它以
POST
字符串值的形式接收ID。该方法的功能是从数据库中获取与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
模型方法:

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
模型方法:

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
但是,我得到了以下错误:

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

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

这些错误意味着
$ids
变量不是字符串格式(意思是:
1,2,3,4
。它可能是一个数组)。要修复它,首先使用
print\r($ids)
var\u dump($ids)
找出它的格式。了解格式后,使用PHP函数(如
内爆()
)将其转换为字符串。

中的
正在寻找格式为的集合,例如
(1,2,3,4)


使您的post值采用
1,2,3,4
格式,以便在查询中包含该值时,该值是有效的,或者如果要发送数组,请使用PHP中的方法,例如
内爆(“,”,$array)
要创建所需的格式以绑定到查询中..

我查看了您的两个函数,发现在这两个函数中,您都将tbl_transactions.id放在where子句中,并且id是从post传递过来的,因此不需要调用get_这些id来获取id,您已经在post数组中有了id,因此不再使用transactions变量,只需使用$this->input->post('id')来补偿这些函数


如果您确实想从get_This_id函数中获取tbl_事务,那么您需要在get_This_id函数的查询中使用group_concat with group by,只需在get_This_id函数中返回该concated id即可。

您的帖子id的格式是什么?请详细说明$ids format?数组或可能是其他可能性