【发布时间】:2021-11-24 08:15:20
【问题描述】:
如何在 PHP 中调用带有多个参数的存储过程?
假设我在 mysql 中有一个这样的存储过程:
delimiter //
create procedure createInvoice (inProduct varchar(20), inCost float, inAmount integer)
begin
.....
.....
.....
end//
delimiter ;
现在在 PHP 中,我想调用此过程并将 inProduct、inCost 和 inAmount 变量设置为来自表单的值。我该怎么做呢?我试过这样设置,但没有成功。
.....
.....
.....
<?php
if(isset($_POST['invoiceform']) {
$sp = 'call createInvoice("' . $_POST['product'] . ',' . $_POST['cost'] . ',' . $_POST['amount'] . '")';
.....
.....
.....
}
?>
所以我只想知道如何在 PHP 中制定“调用”代码。我知道的其余部分(表格、通话后要写的内容等)。
更新: 所以我现在是这样的:
if(isset($_POST['invoiceform']) {
$sp = 'call createInvoice(?,?,?);
$spParameter([$_POST['product], $_POST['cost'], $_POST['amount'];
$stmt = $pdo->prepare($call);
$stmt->bindParam('inProduct', $_POST['product']);
$stmt->bindParam('inCost', $_POST['cost']);
$stmt->bindParam('inAmount', $_POST['amounnt']);
$stmt->execute();
我收到此错误: 警告:PDOStatement::execute(): SQLSTATE[HY093]: 无效参数编号:参数未在 x/x/x/x/x/invoice.php 行 x PDOStatement Object ([queryString] => CALL createInvoice( ?,?,?))
更新 2:解决了:
if(isset($_POST['invoiceform']) {
$sp = 'call createInvoice(?,?,?);
$spParameter([$_POST['product], $_POST['cost'], $_POST['amount'];
$stmt = $pdo->prepare($call);
$stmt->bindParam('inProduct', $_POST['product']);
$stmt->bindParam('inCost', $_POST['cost']);
$stmt->bindParam('inAmount', $_POST['amounnt']);
$stmt->execute($spParameters);
【问题讨论】: