Why does the $ stmt - execute () statement return false in php?

0

I am working with PDO in PHP I have a CRUD for the consultations, in the view I call the controller and in the controller I call the CRUD model to execute the sentence, I do not know why it is returning false, the code is the following :

$stmt = Conexion::conectar()->prepare("INSERT INTO $tabla(nombre, 
    paterno, 
    fechaReserva, 
    correo, 
    checkin, 
    noches, 
    personas, 
    comentarios, 
    huesped satisfecho, 
    habitacion) VALUES(:nombre,
                       :paterno,
                       :fechaReserva,
                       :correo,
                       :checkin,
                       :noches,
                       :personas,
                       :comentarios,
                       :satisfecho,
                       :habitacion)");

    $stmt -> bindParam(":nombre", $datosModel["nombre"], PDO::PARAM_STR);
    $stmt -> bindParam(":paterno", $datosModel["paterno"], PDO::PARAM_STR);
    $stmt -> bindParam(":fechaReserva", $datosModel["fechaReserva"], PDO::PARAM_STR);
    $stmt -> bindParam(":correo", $datosModel["email"], PDO::PARAM_STR);
    $stmt -> bindParam(":checkin", $datosModel["checkin"], PDO::PARAM_STR);
    $stmt -> bindParam(":noches", $datosModel["numeroNoches"], PDO::PARAM_STR);
    $stmt -> bindParam(":personas", $datosModel["numeroPersonas"], PDO::PARAM_STR);
    $stmt -> bindParam(":comentarios", $datosModel["comentarios"], PDO::PARAM_STR);
    $stmt -> bindParam(":satisfecho", $datosModel["satisfecho"], PDO::PARAM_STR);
    $stmt -> bindParam(":habitacion", $datosModel["habitacion"], PDO::PARAM_STR);

var_dump($datosModel);
var_dump($stmt -> execute());

It is worth mentioning that the $ dataModel array that receives the function as a parameter together with $ table brings the data that I send to the controller, it does not mark any error simply by executing false returns, the var_dump of the array has the following information:

array (size=16)
  'nombre' => &string 'prueba' (length=6)
  'paterno' => &string 'prueba' (length=6)
  'fechaReserva' => &string 'prueba' (length=6)
  'email' => &string 'prueba' (length=6)
  'habitacion' => &string 'habitacion' (length=10)
  'canalReservacion' => &string 'Selecciona' (length=10)
  'descuento' => &string 'prueba' (length=6)
  'costoTotal' => &string 'prueba' (length=6)
  'presento' => &string 'si' (length=2)
  'checkin' => &string 'prueba' (length=6)
  'numeroNoches' => &string 'prueba' (length=6)
  'numeroPersonas' => &string 'prueba' (length=6)
  'tipoPago' => &string 'default' (length=7)
  'comentarios' => &string 'prueba' (length=6)
  'pagoObligatorio' => &string 'si' (length=2)
  'satisfecho' => &string 'si' (length=2)
    
asked by Yhoshua Ochoa 12.02.2018 в 09:55
source

1 answer

0

First of all when building a connection by PDO, you could add the attribute PDO::ATTR_ERRMODE to have report of exceptions, warnings. which will cause it to show before var_dump if any exception has occurred as it seems to be the case.

$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

Secondly, directly attacking the problem, the error may be in the syntax of insert having a column with space (satisfied guest) which could be solved by adding a backtick to the names of the columns so that MySQL takes it as a literal string and its utility is as for these cases when you have tables or columns with spaces. (your prepared statemet could be left)

 $stmt = Conexion::conectar()->prepare("INSERT INTO $tabla('nombre', 'paterno', 
                 'fechaReserva','correo', 'checkin', 'noches', 'personas', 
                 'comentarios', 'huesped satisfecho', 'habitacion')

               VALUES(:nombre,:paterno,:fechaReserva,:correo,
                     :checkin,:noches,:personas,:comentarios,:satisfecho,:habitacion)");
    
answered by 12.02.2018 в 14:13