PDO DAO PHP doubts on Insert

Asked

Viewed 96 times

0

I’m trying to make an Insert using Pdo and dao, I have little knowledge in the case, I did on top of a model that works very well, only when I do with more fields and another bank the code does not give error, but does not return anything and does not Insert.

public function insert() {

    $sql = new Sql();

    $results = $sql->select("CALL sp_clientes_insert(:NOME, :EMAIL, :SENHA, :TELEFONE, :ENDERECO, :CPF, :COMPLEMENTO, :NUMERO, :BAIRRO, :CIDADE, :ESTADO, :CEP)" ,
    array(
        ':NOME'=>$this->getNome(),
        ':EMAIL'=>$this->getEmail(),
        ':SENHA'=>$this->getSenha(),
        ':TELEFONE'=>$this->getTelefone(),
        ':ENDERECO'=>$this->getEndereco(),
        ':CPF'=>$this->getCpf(),
        ':COMPLEMENTO'=>$this->getComplemento(),
        ':NUMERO'=>$this->getNumero(),
        ':BAIRRO'=>$this->getBairro(),
        ':CIDADE'=>$this->getCidade(),
        ':ESTADO'=>$this->getEstado(),
        ':CEP'=>$this->getCep()
    ));

    if (count($results) > 0){

        $this->setData($results[0]);
    }

}

<?php

class Sql extends PDO {

    private $conn;
    private $dbHost   = "localhost";
    private $dbUsername = "root";
    private $dbPassword = "";
    private $dbName     = "banco_bd";

    public  function __construct() {
        $this->conn = new PDO("mysql:host=" . $this->dbHost . ";dbname=" . $this->dbName . ";charset=utf8", $this->dbUsername, $this->dbPassword);

    }

    private function setParams($statement, $parameters = array()) {

        foreach ($parameters as $key => $value) {

            $this->setParam($statement, $key , $value);
        }

    }

    private function setParam($statement, $key, $value) {

        $statement->bindParam($key, $value);

    }

    public function query($rawQuery, $params = array()) {

        $stmt = $this->conn->prepare($rawQuery);

        $this->setParams($stmt, $params);

        $stmt->execute();

        return $stmt;

    }


    public function select($rawQuery, $params = array()):array
    {

        $stmt = $this->query($rawQuery, $params);

        return $stmt->fetchAll(PDO::FETCH_ASSOC);

    }

}
?>

PROCEDURE

CREATE DEFINER=`usuario`@`%` PROCEDURE `sp_clientes_insert`(
    pnome varchar(255),
    pemail varchar(255),
    psenha varchar(255),
    ptelefone varchar(255),
    pendereco varchar(255),
    pcpf varchar(14),
    pnumero varchar(50),
    pcomplemento varchar(100),
    pbairro varchar(255),
    pcidade varchar(255),
    pestado varchar(2),
    pcep varchar(8)
)
BEGIN

INSERT INTO clientes (nome, email, senha, telefone, endereco, cpf, numero, complemento, bairro, cidade, estado, cep, notas)
    values (pnome, pemail, psenha, ptelefone, pendereco, pcpf, pnumero, pcomplemento, pbairro, pcidade, pestado, pcep, pnotas);

SELECT * FROM clientes WHERE id = LAST_INSERT_ID();

END
  • On the Internet of your procedure there is a field called pnotas, but you’re not passing it on array, you need to check this.

  • Buddy, activate PDO exception mode. So, every time you have any error regarding the query, it will trigger information content the error and you will find the solution easily. http://php.net/manual/en/pdo.error-handling.php

  • Thanks for the help, the two answer really solved my problem. thank you.

No answers

Browser other questions tagged

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