Welcome Guest, Not a member yet? Register   Sign In
$builder->insert($data); not work
#1

(This post was last modified: 08-07-2020, 07:06 AM by darkstaring.)

Hi all,
I have a problem with a insert in a database.

This is the controller:
PHP Code:
......

$AmministrazionenModel = new AmministrazioneModel();

$data = [
        'usato'         => $myObject["usato"],
        'nome'          => $myObject["nome"],
        'descrizione'   => $myObject["desc"],
        'targa'         => $myObject["targa"],
        'telaio'        => $myObject["telaio"],
        'km'            => $myObject["km"],
        'id_veicoli_cat'=> $myObject["veicoli_cat"],
        'id_zona'       =>  ($session->id_concessionaria),
        'venduto'       => 0
      
];

$res $AmministrazionenModel->insert_auto($data);
...... 



This is model:
PHP Code:
  public function insert_auto($data)
  {
       $db      = \Config\Database::connect();
       $builder $db->table("veicoli");
       $builder->insert($data);
       print $builder->getCompiledSelect();
  

The query printed is "SELECT * FROM `veicoli`"
instead of
"INSERT INTO veicoli ....."

Why ?
Can you help me ?
--------------

Edit:
Codeigniter 4.0.4
Reply
#2

Because you are using getCompiledSelect. That returns the SELECT query. Try getCompiledInsert instead.
Reply
#3

(This post was last modified: 08-18-2020, 07:42 AM by darkstaring.)

Thank you!

I make a partially fix.. but now I have this error:

    "message": "Call to undefined method CodeIgniter\\Database\\MySQLi\\Connection::insert()"


I think that the error isn't in insert but in the connection:
PHP Code:
       $db      = \Config\Database::connect();
       $builder $db->table("veicoli");
       $builder->insert("veicoli"$data); 
Reply
#4

Can't really tell why it's not connecting. Enabled development environment and check the logs. You are however using insert() incorrectly.
https://codeigniter.com/user_guide/datab...rting-data
Reply




Theme © iAndrew 2016 - Forum software by © MyBB