Insert data where the already defined id exists! in codeigniter

1

I hope you had a nice Christmas.

It turns out that I have a table called votes with the following fields:

id , option , name , mail , rut , cond1 , cond2 , date

is a voting system then when voting is saved in the db the ID and the opcion later if you want the individual is registered and for that I need a INSERT name, run .. WHERE ID = ID

try with this code: model

function InsertRegistro($id,$data){
   $this->db->insert('votaciones',$data);
   $this->db->where("id",$id);
}

from my controller:

public function registro() {

    $id = $this - > input - > post('id');

    $data = array(

        'nombre' => $this - > input - > post('nombre'),
        'correo' => $this - > input - > post('correo'),
        'rut' => $this - > input - > post('rut'),
        'cond1' => $this - > input - > post('cond1'),
        'cond2' => $this - > input - > post('cond2'),

    );

    $this - > load - > model('Prontomodel');
    $this - > Prontomodel - > InsertRegistro($id, $data);

}

I insert data but I delete the id and then it does not let me insert, something I'm doing wrong, the thing is that I'm learning codeigniter and I would appreciate it if you could teach me how to do it, thanks.

    
asked by Hernan Humaña 26.12.2016 в 14:54
source

1 answer

1

As far as I can understand, really what you want to do is an update of a database record, not an insertion of a new record. In fact, in MySQL the clause WHERE can not be used in a data entry, so I suppose it will be similar here (I do not know which database engine uses codeigniter ).

Therefore, you would have to use a UPDATE (update) instead of a INSERT (insertion), in which if the clause WHERE is allowed.

Your code would be, according to the documentation , of this mode:

function InsertRegistro($id,$data){
   $this->db->where('id',$id);
   $this->db->update('votaciones',$data);
}
    
answered by 26.12.2016 / 15:46
source