phppdomariadbsliminsert-statement

Error: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined PDO


I´m new in PHP, and I´m developing an API with SlimFramework 3

I have this POST method

$app->post('/factura', function(Request $request, Response $response){
    $conn = PDOConnection::getConnection()
    try{
        $sql = "INSERT INTO bd_zapateria.factura
        (
          idCliente,
          idEmpleado,
          fechaFactura,
          subTotal,
          descuento,
          Impuesto,
          totalFactura)
          VALUES
          (:idCliente,
          :idEmpleado,
          :fechaFactura,
          :subtotal,
          :descuento,
          :Impuesto,
          :totalFactura)";

          $stmt = $conn->prepare($sql);

          $stmt->bindParam(':idCliente', $idCliente);
          $stmt->bindParam(':idEmpleado', $idEmpleado);
          $stmt->bindParam(':fechaFactura', $fechaFactura);
          $stmt->bindParam(':subTotal', $subTotal);
          $stmt->bindParam(':descuento', $descuento);
          $stmt->bindParam(':Impuesto', $Impuesto);
          $stmt->bindParam(':totalFactura', $totalFactura);

          $idCliente = (int) $request->getParam("idCliente");
          $idEmpleado = (int) $request->getParam("idEmpleado");
          $fechaFactura = $request->getParam("fechaFactura");
          $subTotal = (int) $request->getParam("subTotal");
          $descuento = (int) $request->getParam("descuento");
          $Impuesto = (int) $request->getParam("Impuesto");
          $totalFactura = (int) $request->getParam("totalFactura");


          $result = $stmt->execute();
          echo "New records created successfully";
          echo($result);
          $data['Factura'] = $result;

          $response = $response->withHeader('Content-Type','application/json');
          $response = $response->withHeader('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, Accept, Origin, Authorization');
          $response = $response->withHeader('Access-Control-Allow-Origin', '*');
          $response = $response->withHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, PATCH, OPTIONS');
          $response = $response->withStatus(200);
          $response = $response->withJson($data);
          return $response;
         }catch (PDOException $e) {
           $this['logger']->error("DataBase Error.<br/>" . $e->getMessage());
           echo "Error: " . $e->getMessage();
         } catch (Exception $e) {
           $this['logger']->error("General Error.<br/>" . $e->getMessage());
           echo "Error: " . $e->getMessage();
         } finally {
          // Destroy the database connection
           $conn = null;
      }
}); 

When I try to send values, using Postman, it returns this error:

Error: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined

I have the correct headers in Postman


Solution

  • The placeholder :subtotal doesn't exist in the bindings. The bound name is :subTotal. Correct the case in the query, or the binding, and it should work.

    $stmt->bindParam(':subtotal', $subTotal);