Insertion with PDO

Asked

Viewed 46 times

0

I’m using the PHP with the objects PDO and Pdostatement, and my code does not portray any errors or Warning, I did the verification of the variables were passing with print_r and is normally, but does not insert into the seat.

<?php

require_once('conexao.php');

$nome = $_POST["nome"];
$sobrenome = $_POST["sobrenome"];
$cep = $_POST["cep"];
$estado = $_POST["estado"];
$cidade = $_POST["cidade"];
$telefone = $_POST["telefone"];
$modelo = $_POST["modelo"];
$marca = $_POST["marca"];
$fabricacao = $_POST["fabricacao"];
$foto = $_FILES['file'];

    $extensao = strtolower(substr($_FILES['file']['name'], -4));
    $novo_nome = md5(time()) . $extensao;
    $diretorio = "img/";
    move_uploaded_file($_FILES['file']['tmp_name'], $diretorio.$novo_nome);

$stm = new PDOStatement();

try {

    //Inicia a Transição
    $dbh->beginTransaction();

    /*Instrução SQL */
    $sql = "INSERT INTO estado(estado) VALUES (:estado)";

    /*Prepara o comando SQL */
    $stm = $dbh->prepare($sql);

    /*Pega os parâmetros e os avalia*/
    $stm->bindParam(":estado", $estado);

    /*Executa o comando SQL */
    $dbh->exec($sql);

    /*Pega o último ID colocado no banco de dados */
    $idEstado = $dbh->lastInsertId();

    $sql = "INSERT INTO cidade(cidade, Estado_idEstado) VALUES (:cidade, :Estado_idEstado)";
    $stm = $dbh->prepare($sql);
    $stm->bindParam(":cidade", $cidade);
    $stm->bindParam(":Estado_idEstado", $idEstado);
    $dbh->exec($sql);
    $idCidade = $dbh->lastInsertId();

    $sql = "INSERT INTO cep(:cep, Cidade_idCidade) VALUES (:cep, :Cidade_idCidade)";
    $stm = $dbh->prepare($sql);
    $stm->bindParam(":cep", $cep);
    $stm->bindParam(":Cidade_idCidade", $idCidade);
    $dbh->exec($sql);
    $idCep = $dbh->lastInsertId();

    $sql = "INSERT INTO veiculo(:modelo, :marca, :fabricacao, :foto, :descricao) VALUES(:modelo, :marca, :fabricacao, :foto, :descricao)";
    $stm = $dbh->prepare($sql);
    $stm->bindParam(":modelo", $modelo);
    $stm->bindParam(":marca", $marca);
    $stm->bindParam(":fabricacao", $fabricacao);
    $stm->bindParam(":foto", $novo_nome);
    $stm->bindParam(":descricao", $descricao);
    $dbh->exec($sql);
    $idVeiculo = $dbh->lastInsertId();

    $sql = "INSERT INTO usuario(:nome, :sobrenome, :telefone) VALUES(:nome, :sobrenome, :telefone, :Cep_idCep, :Veiculo_idVeiculo)";
    $stm = $dbh->prepare($sql);
    $stm->bindParam(":nome", $nome);
    $stm->bindParam(":sobrenome", $sobrenome);
    $stm->bindParam(":telefone", $telefone);
    $stm->bindParam(":Cep_idCep", $idCep);
    $stm->bindParam(":Veiculo_idVeiculo", $idVeiculo);
    $dbh->exec($sql);

    $dbh->commit();

} catch(PDOException $e){
    echo "Houve algum erro na operação" . $e->getMessage();
    $dbh->rollBack();
}
?>

Some action needed to complete the insertion is missing?

  • 1

    What are the log messages in the server log file? PDO is configured to release exceptions in case of crashes?

  • I’m using XAMP as a server (localhost) in this case, but when I click on log in part in "apache", it only indicates me the same syntax errors (that I had done before), if I put this code, simply no error appears in log. But when I installed XAMP I only enabled ext PDO

  • 1

    In connection via PDO you have set the attribute PDO::ATTR_ERRMODE as being PDO::ERRMODE_EXCEPTION?

  • Okay, I defined it correctly, but it looks like it’s a syntax error SQL in case the SGBD would be the MariaDB: "You have an error in your SQL syntax; check the manual that Corresponds to your MariaDB server version for the right syntax to use near ':status)' at line 1"

  • $dbh->exec($sql) why is running SQL directly instead of the "prepared version" of it, $stm->execute()?

  • I didn’t know the object PDOStatement had a function execute, thought only that the other object (in this case the PDO) had a function of that.

  • connection file is a class?

  • You are adding the two dots in front of the name of the bank column (:). Put the two points only in the "values" as an argument to make the bindParams. Also, in your last query you are adding more data than the past arguments.

Show 3 more comments
No answers

Browser other questions tagged

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