Search code examples
phpmysqlcodeigniter

Problems Trying Insert Data in Database using $this->input->post() - Codeigniter


Im trying use "$this->input->post();" of Codeigniter to do not need specify each field in my form. But im getting troubles when try insert into Database.

Look my controller:

public function cadastrar(){
        $var = $this->input->post(null, TRUE);
        $this->load->model('m_clientes');
        $this->m_clientes->inserir($var);
}

My controller is simplified here because i know how to handle database in codeigniter. The result of this post was:

  Array ( [nome] => Raphael [sobrenome] => Schubert [cpf] => 893.528.432-89 [rg] => 4529875231908472 [telefone] => (53) 2980-5792 [celular] => (53) 9 2180-7529 [rua] => Israel de Almeida [numero] => 859 [cep] => 88.312-000 [bairro] => São Vicente [cidade] => ITAJAÍ [estado] => Santa Catarina [email] => [email protected] [tipo] => pf [cnpj] => 34.827.481/2834-78 [inscricaoestadual] => 34120489032814930128 [razaosocial] => Teste [nomefantasia] => Disney [dataaberturaempresa] => 10/21/15 [proprietario] => Marcos Aurelio )

I normaly use this way to insert:

$data = array(
'user_name' => $this->input->post('user_name',TRUE);
'user_phone' => $this->input->post('user_phone',TRUE);
'user_role' => $this->input->post('user_role',TRUE);
);
$this->name_of_model->inserir($data);

And works...

But i was trying to use just $this->input->post(); to get all fields from form. Because my actualy application will have hundreds of fields and i was trying to do not write each line.

So my model actually was:

   public function inserir($var){
        if($var!=NULL):
            print_r($var);
            $this->db->insert('tb_usuarios',$var);
        endif;
    }

But i`m getting and error saying:

Message: Undefined property: Clientes::$db and Message: Call to a member function insert() on null

My table name is: "tb_usuarios" I changed all fields in database to accept NULL to see if i`m get some field name wrong... but not work...

Any tips??


Solution

  • There is no need to catch the POST var inside $var. You can see POST variable inside the model very well. So all you need to do in the controller is:

    public function cadastrar(){
        $this->load->model('m_clientes');
        $this->m_clientes->inserir();
    }
    

    ,and inside your model:

    public function inserir(){
        if( count($this->input->post()) > 0):
            $this->db->insert('tb_usuarios',$this->input->post());
        endif;
    }
    

    The fields names in your form must correspond to the column names inside your table. The message Message: Call to a member function insert() on null means you forgot to load the database library, just like remiheens said.

    But my advice is, to use form validation for your fields, so you may be sure all necessary fields are completed using the required data format for each one of them. Although this may require allot of coding, there is no other safe way from errors on database operations. You cannot trust the user to insert the data correctly, that's why you need form validation.