Problem when trying to insert into mysql using PDO

Asked

Viewed 258 times

0

I have a problem when I try to insert from a form

<form method="post" action="../../controller/inserirContato.php">
                    <label for="name" class="nameLabel">Meu nome é: </label>
                    <input id="name" type="text" name="nome" placeholder="Seu nome...">
                    <label for="email" class="emailLabel">Meu e-mail é:</label>
                    <input id="email" type="text" name="email" placeholder="Seu e-mail...">
                    <label for="titulo">Estou precisando de: </label>
                    <input id="subject" type="text" name="titulo" placeholder="O que deseja...">
                    <label for="message" class="messageLabel">Minhas observações são: </label>
                    <textarea id="message" name="obs" placeholder="observações..."></textarea>

                    <button type="submit" name="enviar">Enviar</button>

                </form>

the php file I made, this is it.

include '../config/PDOUtil.php';
$con = new PDOUtil();
$nome = $_POST['nome'];
$email = $_POST['email'];
$titulo = $_POST['titulo'];
$obs = $_POST['obs'];

$sql = 'INSERT INTO contato (nome, email, titulo, obs) VALUES (:nome, :email, :titulo, :obs)';
try {

    $recebeConexao = $con->conectar()->prepare($sql); 
    $recebeConexao->bindValue(':nome', $email);
    $recebeConexao->bindValue(':email', $email);
    $recebeConexao->bindValue(':titulo', $titulo);
    $recebeConexao->bindValue(':obs', $obs);
    $recebeConexao->execute();


} catch (PDOException $ex) {
    echo "ta dando pau";
}

thank you.

  • I’m not sure, but it assigns the prepare value to a variable and then executes it with it, e.g.: $Connection = $con->connect(); $Stmt = $connection->prepare(sql); $stmt->execute();

  • 2

    in bindValue(':nome' pass $nome and not $email.

  • For more details of the error, give a $ex->getStackTrace() and stick it here.

1 answer

1

You can do it this way:

$sql = 'INSERT INTO contato (nome, email, titulo, obs) VALUES (:nome, :email, :titulo, :obs)';
try {

    $recebeConexao = $con->conectar()->prepare($sql);

    $recebeConexao->bindParam(':nome', $_POST['nome'], PDO::PARAM_STR);
    $recebeConexao->bindParam(':email', $_POST['email'], PDO::PARAM_STR);
    $recebeConexao->bindParam(':titulo', $_POST['titulo'], PDO::PARAM_STR);
    $recebeConexao->bindParam(':obs', $_POST['obs'], PDO::PARAM_STR);

    $recebeConexao->execute();
} catch (PDOException $ex) {
    echo "ta dando pau";
}

Browser other questions tagged

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