insert only one field with pdo

0

Hey guys, my problem is the following I'm using the PDO object of php to insert records in the database, the problem is when I try to insert only one field in a table example:

$query = 'INSERT INTO alunos (nome) VALUES(:nome)'; $stmt= $conexao->prepare($query); $stmt->bindValue(':nome',$_POST['nome']); $stmt->execute();

Well if I try to insert only one field, it will not work if I specify all of it works example:

$query = 'INSERT INTO alunos (numero,nome,username,senha) VALUES(:numero,:nome,:username,:senha)'; $stmt= $conexao->prepare($query); $stmt->bindValue(':numero',$_POST['numero']); $stmt->bindValue(':nome',$_POST['nome']); $stmt->bindValue(':username',$_POST['username']); $stmt->bindValue(':senha',$_POST['senha']); $stmt->execute();

Am I doing something wrong in the first example?

    
asked by anonymous 04.10.2018 / 16:54

1 answer

0

missing a parenthesis in the expensive end, edit: you need to close the sql statement and iron the prepare parentheses

and try to do so:

$variavel = filter_var($_POST['nome']);
$stmt = $conexao->prepare("INSERT INTO alunos (nome) VALUES(:nome)");
$stmt->bindParam(':nome', $variavel);
$stmt->execute();
    
04.10.2018 / 17:01