Error inserting data into MySQL - CodeIgniter

0

I have the code

<?php
$chave = $retorno->resposta->cobrancasGeradas->cliente->cobranca->chave;

$this->db->insert('boletos', array('id_fatura'=>$id_fatura, 'chave_boleto'=>$chave));
?>

When it executes this insert it returns the error:

Error Number: 1054

Unknown column 'HILO3' in 'field list'

INSERT INTO 'boletos' ('id_fatura', 'chave_boleto') VALUES (20, 81954-30765231-HILO3)

Filename: /var/www/html/Cotas/models/conta_model.php

Line Number: 168

The type field of chave_boleto in MySQL is varchar(100)

    
asked by anonymous 05.01.2016 / 00:14

3 answers

0

The problem is basically in the query, it seems that it is trying to insert the value of a string, like number, and gives error, try casting a type:

<?php
$chave = $retorno->resposta->cobrancasGeradas->cliente->cobranca->chave;

$this->db->insert('boletos', array('id_fatura' => $id_fatura, 'chave_boleto' => (string) $chave));
?>

or this:

 $this->db->insert('boletos', array('id_fatura' => $id_fatura, 'chave_boleto' => "{$chave}"));
    
05.01.2016 / 13:55
0

You can do it this way:

<?php
     $chave = $retorno->resposta->cobrancasGeradas->cliente->cobranca->chave;
     $dados['id_fatura'] = $id_fatura;
     $dados['chave_boleto'] = $chave;
     $this->db->insert('boletos', $dados);
?>
    
05.01.2016 / 01:28
0

Just adapt to your mood.

Correct way with PDO. The good thing is you do an abstract class with the CRUD (abstract class Crud extends DB) put this code and replace the values with variables, then just call the crud in your class you want to do the insert.

But not thick is there.

    try {
            DB::getInstance()->beginTransaction();

            $sql = "INSERT INTO boletos (id_fatura,chave_boleto) VALUES (:id_fatura, :chave_boleto)";
            $stmt = DB::prepare($sql);
            $dados = seuArray //array('id_fatura'=>$id_fatura, 'chave_boleto'=>$chave)

            foreach ($dados as $key => $v) {
                $stmt->bindValue(':' . $key, $v);
            }
            //ou assim
            //$stmt->bindValue(':id_fatura', 1);
            //$stmt->bindValue(':chave_boleto', 'fjçalkdfjoajlaksdf');


            if ($stmt->execute()) {
                $cd = DB::getInstance()->lastInsertId();
                modalSalvo("Salvo com sucesso!");
                return $cd;
                DB::getInstance()->commit();
            }
        } catch (PDOException $e) {
            echo '<br />Error: ' . $e->getMessage();
        }
    
05.01.2016 / 14:43