错误:SQLSTATE [HY093]:参数号无效:参数未定义PDO

时间:2018-05-27 19:55:17

标签: php pdo mariadb slim insert-statement

我是PHP的新手,我正在使用SlimFramework 3开发API

我有这个POST方法

$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;
      }
}); 

当我尝试使用Postman发送值时,它会返回以下错误:

  

错误:SQLSTATE [HY093]:参数号无效:参数不是   定义

我在Postman中有正确的标题

1 个答案:

答案 0 :(得分:2)

占位符numpy不存在于绑定中。绑定名称为:subtotal。更正查询或绑定中的大小写,它应该可以正常工作。

:subTotal