Insert data into the bank with codeigniter

0

I need to display and save data in my database but as I'll show below I'm using Joins to display, for example the name of an author using their primary key. For display is working, but when I use my method to insert data into the table it returns an error with the foreign key, so I could understand my code is trying to insert the returned name as if it were the foreign key as this key will not go there is error, below code and images of what I'm doing:

My screen where the author's name is displayed:

Mycodeusedtoreturntheabovescreendata:

functionhistory($ccod){$this->db->select('dat,cliente,texto,comcod,cnome,username');$this->db->where('cliente',$ccod);$this->db->from('comentarios');$this->db->join('clientes','clientes.ccod=comentarios.cliente');$this->db->join('users','users.id=comentarios.autor');$query=$this->db->get();return$query->result();}

Mycodetoinsertthedataintothedatabase:

functioninserir_coment($data){return$this->db->insert('comentarios',$data,"autor.comentarios = user.id");
    }

Error returned while executing this code:

I think that by typing the syntax to insert the data my problem is solved, but as I am beginner I could not solve it myself, for this application I am using Codeigniter 3.1.3.

    
asked by anonymous 23.01.2017 / 19:52

1 answer

3

The error is saying that in the comentarios table the autor column is a foreign key that references the users table. That is, what you really said.

There must be user with código fmlima4 as it is in the insert, or need to pass the user code.

The third argument here does not make sense, I have never seen three arguments for insert , with the third being where .

return $this->db->insert('comentarios', $data, "autor.comentarios = user.id");

I think you wanted to do this

 $data["autor"] = $algumValorDaSessaoPorExemplo;
 return $this->db->insert('comentarios', $data);

You can do this in your View, for example, adding a input of type hidden to save the author code, and in your controller where you mount the array $data assign the value of that hidden to the key autor

    
23.01.2017 / 20:02