gpt4 book ai didi

php - 使用 Codeigniter 更新 mysql 数据库

转载 作者:行者123 更新时间:2023-11-29 21:49:48 25 4
gpt4 key购买 nike

我一直在尝试更新数据库中的记录,但我得到的 echo 是该操作尚未更新,我想要的只是更新数据库中的余额字段!以下是 Controller 中的代码,我在其中获取从 ajax 传递的值,并将电子邮件和数据传递给模型。 Controller 。

 public function transfer_amount(){


$email = $this->input->post('view');



$data = array(

'balance' => $this->input->post('amount'),

);

$this->load->model('user_model');


if($this->user_model->transfer_amount($data,$email)){


echo'unsuccessfull';


}
else
{

$this->load->view('layer_service/success',$data);

}



}

需要更新数据库字段的模型如下。

   function transfer_amount($data,$email){



$this->db->where('email', $email);

$this->db->update('tbl_users', $data);

// Code here after successful insert
// to the controller

return true;


}

最佳答案

根据您的上述评论,您需要修剪电子邮件中的空格。

function transfer_amount($data, $email) {

$this->db->update('tbl_users', $data, array(
'email' => trim($email),
));

// Code here after successful insert
// to the controller

return true;
}

另外,在您的 Controller 中,您需要替换此行

if($this->user_model->transfer_amount($data,$email)){

if( ! $this->user_model->transfer_amount($data,$email)) {

因为您的模型在成功时返回 TRUE,但您的 Controller 期望在成功时返回 FALSE

您还可以使用简单的 if 语句(例如

)检查更新是否成功,并且确实更改了数据库中的某些行
if(0 >= $this->db->affected_rows()) {
// update unsuccessful
}

更新

如何在 MySQL 中添加当前余额。您只需添加到数据库中已有的值即可。

$data = array(
'balance' => 'balance + ' . $this->input->post('amount'),
);

关于php - 使用 Codeigniter 更新 mysql 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33760244/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com