Problems with inserting and receiving MySQL values with CodeIgniter

0

I'm using the CodeIgniter framework, I started my studies a few days ago. I am facing some problems for creating a CRUD. Some of them is the connection to the bank. I would like to insert the values in the table, but it will not, and when I insert it by the terminal and printar in PHP it looks like this:

model_Conta.php:

classModel_contaextendsCI_Model{public$id;public$nome;public$idade;public$profissao;publicfunctionconexao(){try{$conexao=newPDO("mysql:host=localhost;dbname=ListaContatos;charset=utf8", "joao", "");
                return $conexao;

                }catch(PDOException $erro){
                echo "Conexao falhou: ". $erro->getMessage();
            }
        }

        public function inserir($nome, $idade, $profissao){
            $PDO = Model_conta::conexao();
            $PDO->query("INSERT INTO contatos(nome, idade, profissao) VALUES ($nome, $idade, $profissao);");
        }

        public function retorna(){
            $PDO = Model_conta::conexao();
            $contatos = $PDO->query("select * from contatos");    
            foreach($contatos as $contato){
                print_r('<pre>');
                print_r($contato);
            }
        }
    }

Base.php:

public function cadastrar(){
    $nome = $this->input->post('nome');
    $idade = $this->input->post('idade');
    $profissao = $this->input->post('profissao');
    $this->load->model('Model_conta');
    $this->Model_conta->inserir($nome, $idade, $profissao);

}
public function retorna(){
    $this->load->model('Model_conta');
    $this->Model_conta->retorna();
}

Errors:

  

Return of table with more values;       You are not entering the values in the bank

    
asked by anonymous 27.10.2018 / 01:26

1 answer

0

Friend, you've started totally wrong. No is for you to create a PDO connection within a model!

You have to use the CodeIgniter database connection, to see how you configure you can see this link:

link

For some examples of how to use it, you can use this link:

link

All documentation on database codeigniter, find this link:

link

* You are naming the Model incorrectly, you have to use Nome_model and not Model_nome .

    
27.10.2018 / 02:11