Welcome Guest, Not a member yet? Register   Sign In
Update some content
#1

[eluser]sam_weiss[/eluser]
Hi people, i'm new in codeigniter and i'm very excited.
But now I got stuck on a problem. I am not able to edit the content via a form.

Controller:
Code:
class Site extends CI_Controller{
    
function index(){
    $data['records'] = $this->data_model->get();
    $this->load->view('home', $data);
    
    
}

function delete(){
    
  $this->data_model->delete();
  redirect('site/index');
    
}
function add(){
    $tables['title'] = $this->input->post('title');
    $tables['content'] = $this->input->post('content');
    $tables['id'] = $this->input->get('entry_id');
   $this->data_model->add($tables);
   redirect('site/index');
}

function update(){
    
    $tables['title'] =  $this->input->post('title');
    $tables['content'] = $this->input->post('content');
   $this->data_model->update($tables);
  redirect('site/index');
  
}


function comments(){
    
     $this->load->view('commentsview');

}

Function update in Model:
Code:
function update($tables){
    
    
      $this->db->set('id', $this->uri->segment(3));
      $this->db->update('data', $tables);
  }


Commentsview(View):
Code:
<?php echo form_open('site/update'); ?>
<label for="title">Title:</label>
&lt;input type="text" name="title" /&gt;
<label for="content">Content:</label>
&lt;input type="textarea" rows="10" name="content"&gt; &lt;/textarea&gt;
<p>&lt;input type="submit" value="Enviar" /&gt; </p>
&lt;?php echo form_close(); ?&gt;

So it gives me this error:
Quote:A Database Error Occurred
Error Number: 1062

Duplicate entry '0' for key 1

UPDATE `data` SET `id` = 0, `title` = 'title', `content` = 'content'

Filename: C:\xampp\htdocs\ci\system\database\DB_driver.php

Line Number: 330

Someone help me? And sorry my english.
#2

[eluser]samitrimal[/eluser]
Change in the model
Code:
function update($tables){
    
    
      $this->db->set('id', $this->uri->segment(3));
      $this->db->update('data', $tables);
  }

to
Code:
function update($tables){
    
    
      $this->db->where('id', $this->uri->segment(3));
      $this->db->update('data', $tables);
  }
#3

[eluser]sam_weiss[/eluser]
Hi samitrimal,

I had already tried that and it did not work. When i trying edit, sometimes works but field of id is 0.
#4

[eluser]Twisted1919[/eluser]
Where you set your "where condition" ?
You update all the records from the table and set them same data(ie: id=0) so yes, it's obvious you get an error.


LE: the method posted was wrong.
#5

[eluser]LuckyFella73[/eluser]
In your edit form you need a hidden form field
and set the value equal to the id of the record
you are editing.

When calling the update method (after submitting the form)
you need a line like:
Code:
$tables['id'] = $this->input->post('row_id');
// assuming your hidden field has the name "row_id" in this example

Otherwise your model doesn't know which row has to be updated.

If you modify your script like that the update method from your model
should work like you tried inbetween and samitrimal suggested:
Code:
function update($tables){
    
    
      $this->db->where('id', $this->uri->segment(3));
      $this->db->update('data', $tables);
  }
#6

[eluser]sam_weiss[/eluser]
Still don't working. :/
#7

[eluser]LuckyFella73[/eluser]
sorry I didn't check the model code carefully enough:
Code:
function update($tables){
      $this->db->where('id', $tables['id']); // use the value from hidden input field here
      $this->db->update('data', $tables);
  }




Theme © iAndrew 2016 - Forum software by © MyBB