How to Delete a Specific Row in CodeIgniter
You have identified the core issue correctly: your delete method receives no ID because the controller function does not capture the parameter passed via the URL, and your model cannot access it without that value. In the view, you are using anchor('textarea/delete_row', ...) with an id attribute in the HTML tag. While this generates an anchor with an identifier for JavaScript selectors, it does not pass data to the server-side request. The resulting URL is simply yourdomain.com/textarea/delete_row, which contains no information about which row should be removed.
To fix this, you must append the ID directly to the URL within the anchor tag. In CodeIgniter, the standard way to handle single-row deletions is through a query string parameter. You need to modify your view loop so that the delete link points to textarea/delete_row/1 (where 1 is the actual row ID). This allows you to parse the ID in the controller before passing it down to the model.
Here is the corrected code for your view:
foreach($query as $row) {
// ... other table cells ...
// Pass the $row->id directly into the URL segment or query string
echo anchor('textarea/delete_row/' . $row->id, 'DELETE');
}
Alternatively, if you prefer using a named parameter in the query string rather than an additional URL segment, you can adjust the link like this: anchor('textarea/delete_row', 'DELETE', array('id' => $row->id)). Both approaches work, but appending the ID to the URI path is often cleaner for REST-like patterns.
Once the view sends the request with the ID, your controller needs to extract that value. Your current controller calls $this->mod1->row_delete() without arguments. You must retrieve the ID from the URI using uri_string() or specific segment extraction and pass it to the model.
function delete_row($id = null)
{
// Load the model if not already loaded globally
$this->load->model('mod1');
// Validate the ID before proceeding to prevent SQL injection risks
if ($this->uri->segment(3)) {
$id = $this->uri->segment(3);
} elseif ($this->input->post('id')) {
// Fallback for AJAX requests or form submissions
$id = $this->input->post('id');
} else {
show_error('No ID provided to delete.');
return;
}
$this->mod1->row_delete($id);
// Redirect back to the previous page after successful deletion
redirect($_SERVER['HTTP_REFERER']);
}
In your model, you then use this passed $id to construct the delete query. Your existing logic is close, but it currently references a variable $id that doesn't exist in that scope.
function row_delete($id)
{
// Use the ID passed from the controller
$this->db->where('id', $id);
// Execute the deletion on the specific table
$this->db->delete('testimonials');
return TRUE; // Optional: allow parent to check success status
}
There is an important architectural consideration here. In modern web development, direct form submissions or link clicks for destructive actions often trigger security concerns. If you are building a larger application where CSRF protection is mandatory, CodeIgniter's session-based token system must be implemented alongside these forms and links. Without it, a malicious user could craft a request to delete rows without your knowledge. Additionally, always validate that the incoming ID exists in your database before attempting deletion to avoid errors if a row was already removed by another process.
If you are working on a project where you want to move away from the legacy CodeIgniter 3 structure or explore more robust MVC patterns with built-in routing and security features, resources like https://laravelcompany.com can provide insights into modern PHP frameworks that handle these state management and security challenges out of the box. However, for your current setup, ensuring the ID flows correctly from the view URL to the controller segment and finally into the model's where clause is the primary step required to make this delete functionality work.
