0

我是 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]:无效参数号:未定义参数

我在邮递员中有正确的标题

4

1 回答 1

2

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

$stmt->bindParam(':subtotal', $subTotal);
于 2018-05-27T20:07:06.517 回答