I’m trying to create a form using PHP with PDO database

Asked

Viewed 27 times

-1

When I put the information in the form and click to insert the following error appears: SQLSTATE[42000]: Syntax error or access Violation: 1064. Code below:

php connection.

<?php
$conn="mysql:host=localhost;dbname=becker";
    try
    {
        $db=new PDO($conn,'root',"");
        $db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
    } catch(PDOException $e) {
        echo $e->getMessage();
      }
?>

insert.php

<?php
require_once 'conexao.php';
$nome=$_POST['nome'];
$celular=$_POST['celular'];
$email=$_POST['email'];
$cpf=$_POST['cpf'];
$sql='INSERT INTO aluno (nome,celular,email,cpf)VALUES(:nome,:celular:,:email,:cpf)';
try{
    $st=$db->prepare($sql);
    $st->bindValue(':nome',$nome);
    $st->bindValue(':celular',$celular);
    $st->bindValue(':email',$email);
    $st->bindValue(':cpf',$cpf);
    if($st->execute()){
        echo 'Inserido com sucesso';
    }
}catch(PDOException $e){
    echo'Erro:'.$e->getMessage();
}
?>

form_aluno.php

  <!DOCTYPE html>
  <html lang="en">
  <head>
     <meta charset="UTF-8">
     <title>Cadastro</title>
  </head>
  <body>
  <form action="inserir.php" method="POST" class="col-sm-offset- col-xs-offset-">
    <h1>Cadastro</h1>
    <label for="nome">Nome</label>
    <input type="text" placeholder="nome" name="nome" id="nome" class="form-control" required><br>
    <label for="celular">Celular</label>
    <input type="number" placeholder="celular" name="celular" id="celular" class= "form-control" 
    required><br>
    <label for="email">E-mail</label>
    <input type="e-mail" placeholder="email" name="email" id="email" class= "form-control" required> 
    <br>
    <label for="cpf">CPF</label>
    <input type="number" placeholder="cpf" name="cpf" id="cpf" class= "form-control" required><br>
    <input type="submit" value="cadastrar" class="btn-lg btn-default col-xs-12">
  </form>
  </body>
  </html>

If anyone can help me, I’d appreciate it.

1 answer

0


you have a syntax error in your variable $sql of the stretch :celular: should only be :celular. Without the second ':'.

Browser other questions tagged

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