简体   繁体   English

Codeigniter中的ected_rows()

[英]affected_rows() in codeigniter

Say I have a form which gets populated when clicked on some link and the user is allowed to update the data and save the changes made to those fields. 假设我有一个表单,当单击某些链接时会填充该表单,并且允许用户更新数据并保存对这些字段所做的更改。

$data = array('somevalue, 'somevalue');            
$this->db->where('id', $this->input->post('id'))->update('links', $data);

            if($this->db->affected_rows() == 1){
                return TRUE;
            }else{
                return FALSE;
            }

And when I make some changes and do the save everything works fine because it returns TRUE for affected_rows() == 1 . 当我进行一些更改并保存时,一切工作正常,因为它为affected_rows() == 1返回TRUE What if I dont make any changes and do the save? 如果我不做任何更改并保存该怎么办? No any row is going to be affected and it will return FALSE . 没有任何行会受到影响,它将返回FALSE

What would be the appropriate way to solve this issue? 解决该问题的合适方法是什么? The main problem is that I am displaying the success message only when it returns TRUE . 主要问题是,仅当成功消息返回TRUE时,我才显示它。 So there will be a problem(ie no message is displayed) if no changes are made and save button is clicked. 因此,如果不进行任何更改并单击“保存”按钮,将出现问题(即不显示任何消息)。

Just return true. 只要返回true。

If there is a problem with connecting to your DB you will get an error message before the application reaches that point anyway. 如果连接到数据库有问题,则在应用程序到达该点之前,您都会收到一条错误消息。

It is redundant code. 它是冗余代码。

You should just be able to do 你应该能够做

return $this->db->where('id', $this->input->post('id'))->update('links', $data);

But you will need to check under which scenarios codeigniter return false. 但是您将需要检查在哪种情况下codeigniter返回false。

PS Cannot why not just use form validation in codeigniter to check if the fields are empty or not before going to the model? PS不能为什么不仅仅在codeigniter中使用表单验证来检查字段,然后再去模型?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM