Error: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined PDO
Error: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined PDO
我是 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 发送值时,出现 returns 这个错误:
Error: SQLSTATE[HY093]: Invalid parameter number: parameter was not
defined
我在 Postman
中有正确的 headers
绑定中不存在占位符 :subtotal
。绑定名称是:subTotal
。更正查询或绑定中的大小写,它应该可以工作。
$stmt->bindParam(':subtotal', $subTotal);
我是 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 发送值时,出现 returns 这个错误:
Error: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined
我在 Postman
中有正确的 headers绑定中不存在占位符 :subtotal
。绑定名称是:subTotal
。更正查询或绑定中的大小写,它应该可以工作。
$stmt->bindParam(':subtotal', $subTotal);