问题
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.
Controller: You should not be passing the
$user_id
value to yourdelete
controller via the url. (such assite/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 asnull
.If your database schema cannot store a boolean value (
true
), then it is common to use1
and0
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.
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:
- the
$userId
that is passed to the controller does not exist in the database or - 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.
- the
来源:https://stackoverflow.com/questions/15567097/how-can-i-detect-a-create-update-delete-query-is-successful-in-codeigniter