How can I detect a create, update, delete query is successful in Codeigniter

狂风中的少年 提交于 2021-01-29 21:10:58

问题


I am currently writing a controller method like this:

public function delete($user_id) {
    if ($this->input->server('REQUEST_METHOD')=='POST') {
        $result = $this->Crm_user_model->update($user_id,
                                                array('deleted'=>true));
        if($result) {
            add_flash_message('info', 'deleted');
        } else {
            add_flash_message('alert', 'can not delete');
        }
        //redirect('user/view');
    }
} 

But all result return nothing, even the database(mssql) is changed. How can I know that the update query is success or not?


回答1:


In crm_user_model->update(), return true or false depending on the output of CodeIgniter's update() function:

if ($this->db->update('mytable', $mydata)) {
    // Do some stuff
    return true;
} else {
    // Do some stuff
    return false;
}

Or if you don't need to do anything else in your model, just do this:

return $this->db->update('mytable', $mydata);




回答2:


I have a concern and some advice.

  1. Controller: You should not be passing the $user_id value to your delete controller via the url. (such as site/ci/delete/99) Whenever you are "writing" data to the database/server you should only be using $_POST to transfer the data to the server-side.

    public function softDeleteUser(): void
    {
        $userId = $this->input->post('user_id');
        if (!$userId) {
            add_flash_message('alert', 'Required data not supplied');
        } elseif (!$this->Crm_user_model->update($userId, ['deleted' => true])) {
            add_flash_message('alert', 'Failed to soft delete user');
        } else {
            add_flash_message('info', 'Soft deleted user');
        }
    }
    

    If no user_id is POST'ed, then $userId will be declared as null.

    If your database schema cannot store a boolean value (true), then it is common to use 1 and 0 as boolean equivalents.

    I prefer to write all negative/erroring/non-successful outcomes before successful ones when constructing condition blocks.

    A user authentication/authorization check before allowing the soft deletion of a user would be advisable so that only privileged users have permission to take this action.

  2. Model: Your querying should be done solely in the model. Because it is possible that a query might not have syntax errors, but also makes no change to the database, you must check the process at two points.

    Consider the situations like:

    1. the $userId that is passed to the controller does not exist in the database or
    2. the row containing the $userId is already "soft deleted".

    In both of these cases, the query will not fail, but there will be no affected rows. These are relevant occurrences for your script to differentiate from actual changing updates.

    public function update(int $userId, array $newData): int
    {
        // ensure that no one can ever modify the user_id column value
        unset($newData['user_id']);
    
        if ($this->db->update('user_tablename', $newData, ['user_id' => $userId])) {
            $affectedRows = $this->db->affected_rows();
            if ($affectedRows) {
                // potentially log successful change if your system keeps track of change history
                // $this->Log->userChange($userId, $newData);
            }
            return $affectedRows;
        }
        return 0;
    }
    

    If there is a syntax error, it will be in your error logs.



来源:https://stackoverflow.com/questions/15567097/how-can-i-detect-a-create-update-delete-query-is-successful-in-codeigniter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!