Php 如何利用数据数组中的存储过程处理函数
我创建了一个函数,在将数据保存到sales表之前检查数量。该函数适用于单变量和post变量变为多变量时;它不起作用,因为它们变成了一个数组。有没有一种方法可以利用这个函数背后的思想来处理数组?我们的想法是,对于每一组,比如说$productCode=2&$quantity=10函数检查,然后说$productCode=1&$quantity=20,直到最后一组Php 如何利用数据数组中的存储过程处理函数,php,mysql,function,stored-procedures,Php,Mysql,Function,Stored Procedures,我创建了一个函数,在将数据保存到sales表之前检查数量。该函数适用于单变量和post变量变为多变量时;它不起作用,因为它们变成了一个数组。有没有一种方法可以利用这个函数背后的思想来处理数组?我们的想法是,对于每一组,比如说$productCode=2&$quantity=10函数检查,然后说$productCode=1&$quantity=20,直到最后一组 <?php //Checking quantity availability
<?php
//Checking quantity availability
//Post variables
//$productCode=$_POST['productCode'];
//$quantity=$_POST['quantity'];
//Equivalent variables
$productCode = array(2, 1, 7, 2);
$quantity = array(10, 20, 30, 40);
//My wild thought BUT no LUCK
for ($i = 0; $i < count($productCode); $i++) {
$productCode=$productCode[$i];
$quantity=$quantity[$i];
function CheckIfQuantityIsEnough ($productCode,$quantity)
{
try
{
$QuantityIsEnough = false;
$pdoAssigned = new PDOConfig;
//Works perfectly well for one variable set
$resultQuantityIsEnough = $pdoAssigned->query("call sp_getStockQuantityCount($productCode,$quantity)");
if( $resultQuantityIsEnough->fetchColumn() >0) //the course has been assigned
{
$QuantityIsEnough = true;
}
return $QuantityIsEnough;
}
catch (Exception $e) {
throw $e;
}
}
}//End For statement
//Check also if Quantity is enough for that
if (CheckIfQuantityIsEnough ($productCode,$quantity) == false)
{
$Feedback = "Quantity not enough for this product";
}
?>
//sp_getStockQuantityCount
CREATE PROCEDURE `sp_getStockQuantityCount`(`vStockTypeID` INT, `vQuantity` DECIMAL(9,0))
BEGIN
declare vQuantityNow int;
declare vQuantitySubtract int;
set vQuantityNow=(select Quantity from stockquantitytbl where StockTypeID=vStockTypeID limit 1);
set vQuantitySubtract=(vQuantityNow-vQuantity);
select count(Quantity)
from stockquantitytbl
where StockTypeID=vStockTypeID and vQuantitySubtract>=0;
END
--------编辑------------
我已经删除了该函数,现在只需要使用存储过程,如下所示:
<?php
//Array data
//$productCode = array(2, 1, 7, 2);
//$quantity = array(10, 20, 30, 40);
//Single set
$productCode=7;
$quantity=101;
$sql="call sp_getStockQuantityCount($productCode,$quantity)";
$result = $odb -> query($sql);
if($result->rowcount() > 0) {
foreach ($result as $row) {
$QuantityIsEnough = $row['Quantity'];
if($QuantityIsEnough == 0)
{
$Feedback = "Quantity not enough for this product";
}
else
{
//Insert sale record
}
}
}
?>
一组工作正常,但如上所述,问题仍然存在于阵列数据上 直接使用sql,而不是使用存储过程/函数,这会更容易。您能公布可能会出现的错误吗?您需要什么样的检查?@danblack,在插入新记录之前检查产品数量。@sandepmodak,它不会带来任何错误。直接使用sql而不是使用存储过程/函数,这样会更容易。您能发布可能会遇到的错误吗?您需要什么样的检查?@danblack,在插入新记录之前检查产品数量。@sandepmodak,它不会带来任何错误。