Syntax error when trying to insert data into MYSQL with php

0

When trying to insert the parameters of "User" and "Password" extracted from an HTML form, I miss an error of mySQL Syntax.

This is my code when inserting my code

    require_once ('Conecction.php');
try {
// Iniciar la conexión a la base de datos
    $pdo = new PDO("mysql:host=$db_host;dbname=$db_name;charset=utf8", $db_user, $db_pass);
// Asginar el modo de error Silencio para chequear nosotros mismos los errores
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
// Asignar la codificación de caracteres a UTF-8
    $pdo->exec("SET NAMES 'utf8'");
// Insertar los datos con parámetros preparados
// bindParam para asignar valores en el momento de la ejecución
    $db_sentence = $pdo->prepare('INSERT INTO ' . $db_table . ' (nombre,password)
                                 VALUES ( :nombre, :password');
    $db_sentence->bindParam(':nombre', $nombre, PDO::PARAM_STR);
    $db_sentence->bindParam(':password', $password, PDO::PARAM_STR);
    $db_sentence->execute();

This is "Conection" where I declare my variables for the database.

<?php
$db_host = 'localhost';
$db_user = 'root';
$db_pass = '';
$db_name = 'Juanjo';
$db_table = 'Persona';
$db_sentence = '';
$db_error = '';
?>

This is where I extract the data from the form.

if(isset($_POST['nombre'])==false || $_POST['nombre'] == ''){
    $nombre='No se ha introducido nada';

}else{
    $nombre=trim(strip_tags($_POST['nombre']));
}


if(isset($_POST['password'])==false || $_POST['password']==''){
    $password='No se ha introducido nada';

}else{
    $password=trim(strip_tags($_POST['password']));
}

The error it gives me is "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 2"

    
asked by kimbo 19.03.2018 в 20:32
source

1 answer

1

At first glance it seems to me that you are closing the sentence badly

 $db_sentence = $pdo->prepare('INSERT INTO ' . $db_table . ' (nombre,password)
                             VALUES ( :nombre, :password');

If you look at the single quote it is inside the parenthesis and not outside the parentheses, like this:

 $db_sentence = $pdo->prepare('INSERT INTO ' . $db_table . ' (nombre,password)
                             VALUES ( :nombre, :password)';
    
answered by 19.03.2018 / 21:13
source