Reputation: 1889
Hello guys i have the following model with the fields that i need to update:
public function admin_add_review( $review = array()){
$data = array(
'title' => $review['title'],
'description' => $review['description'],
'category' => $review['category']
);
$query = $this->db->update('reviews', $data);
if( $query){
return true;
}else{
return false;
}
}
and the following controller, here i just send the data to the view.
public function admin_validation()
{
$this->load->library('form_validation');
$this->load->model('model_users');
$review['title'] = $this->input->post('title');
$review['description'] = $this->input->post('description');
$review['category'] = $this->input->post('category');
$this->model_users->admin_add_review( $review );
$this->load->view('admin_page', $review);
}
and the following view. This is a form with the data taken from the controller and when i update this data it updates the hole table with the same descriptions inside.
<?php
$review = $review[0];
echo form_open_multipart('main/admin_validation');
echo validation_errors();
/*echo "<p> Id review</p>";
$data = array(
"name" => "id",
"value" => $review['id']
);
echo form_input($data);*/
echo "<h3>Title: ";
echo "</h3>";
$data = array(
"name" => "title",
"id" => "title-input",
"value" => $review['title']
);
echo form_input($data);
echo "<h3>Category: ";
echo "</h3>";
$data = array(
"name" => "category",
"id" => "category-input",
"value" => $review['category']
);
echo form_input($data);
echo "<h3>Description: ";
echo "</h3>";
$data = array(
"name" => "description",
"id" => "description",
"value" => $review['description']
);
echo form_textarea($data);
echo "<br/>";
echo "<br/>";
echo form_submit('login_submit', 'Update review.');
echo form_close();
?>
When i press the update button it updates all my database table rows! and i just want only the specified one to be updated. Help please.
Upvotes: 0
Views: 8297
Reputation: 1429
If you want to update existing records from the database, you need to pass a where clause to your php function like:
public function admin_add_review($value, $review = array()){
$data = array(
'title' => $review['title'],
'description' => $review['description'],
'category' => $review['category']
);
$this->db->where('key', $value);
$query = $this->db->update('reviews', $data);
if( $query){
return true;
}else{
return false;
}
}
So the $value criteria could be an id of existing review like review_id or anything else.
Event better:
$query = $this->db->update('reviews', $data, array('review_id' => $value));
//$query = $this->db->update(TABLE, NEW_DATA, CRITERIA);
So if your criteria matches 1 row in table, then only 1 row will be updated.
Upvotes: 3