Error inserting data into Mysql - Codeigniter

Asked

Viewed 485 times

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 he runs this insert gives me 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 field type of chave_boleto in Mysql is varchar(100)

  • 1

    That value 81954-30765231-HILO3is going without the quotes and so it’s not a varchar, I don’t quite understand how it works ('chave_boleto'=>$chave), but have it return the quotes. Let’s wait for someone explaining how it is.

3 answers

0


The problem is basically in the query, it seems that he is trying to insert the value of a string as number, and gives error, try to cast 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}"));

0

only adapt to your way.

Correct way with PDO. The good thing would be to make an abstract class with the CRUD (Abstract class Crud extends DB ) put this code and replace the values with variables, then only call crud in your class you want to do the Insert.

but in the 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();
        }

0

You can do it that way:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.