gpt4 book ai didi

php - 运行多个sql,需要先运行一个,然后两个一起运行 PHP

转载 作者:行者123 更新时间:2023-11-29 16:04:24 28 4
gpt4 key购买 nike

我对 PHP 很陌生。我正在尝试运行这些sql。需要先运行第一个sql,以获得需要插入到其余两个sql中的值。

这是我所做的:

$sql = "SELECT (unitPrice) FROM product WHERE productID = '".$pid."'";
$sql1 = "INSERT INTO order (orderID) VALUES ('".$rnd_id."');";
$sql1 .= "INSERT INTO order_details (orderID,orderType,orderDate,orderTime,quantity,orderPrice,discount,discountPrice,employeeID) VALUES ('".$rnd_id."','".$type."','".$date."','".$time."','".$quantity."','".$oprice."','".$dis."','".$dprice."','".$eid."')";

if($result = mysqli_query($link, $sql)){
while($row = mysqli_fetch_array($result)){
$price = $row['unitPrice'];
$oprice = (double)$price * (double)$quantity;
}
} else{
echo "Something went wrong. Please try again later.";
}

$dprice = $oprice - $oprice * $dis;

//Free result set
mysqli_free_result($result);

if(mysqli_multi_query($link, $sql1)){
do {
/* store first result set */
if ($result = mysqli_store_result($link)) {
mysqli_free_result($result);
}
} while (mysqli_next_result($link));
//Records created successfully. Redirect to landing page
header("location: index5.php");
exit();
} else{
echo "Something went wrong. Please try again later.";
}

最佳答案

首先:您确实应该使用准备好的语句。它不仅更安全,而且还使您的生活更轻松,因为您可以编写更简洁的代码。 Read this关于原因和 this page关于如何。在下面的示例中,我使用 PDO。

您的代码的第一个问题是:您在定义变量之前使用了变量。看看

$sql1 .= [...] VALUES 
('".$rnd_id."','".$type."','".$date."','".$time."','".$quantity."',
'".$oprice."','".$dis."','".$dprice."','".$eid."')";
使用了

$oprice$dprice,但它们仅稍后在代码中定义。因此,它们在您构造查询时没有值。

第二:看看reserved words在mysql中。ORDER 是保留字,不应将其用作表名称。

然后,您使用单独的SELECT查询来获取价格,然后在INSERt查询中使用它。没有必要。数据库非常擅长组合表。因此,您可以将 SELECT 与 INSERT 结合使用,将 product 中的价格与 order_details

中的 INSERT 相结合

我认为变量 $rnd_id、$type、$quantity、$dis 是在其他地方定义的,因为代码中没有显示。

使用准备好的语句,插入$rnd_id可以这样写:

//construct a query with :rnd_id as a parameter
$query= 'INSERT INTO order_id (orderID) VALUES (:rnd_id)';

//bind the variable to the parameter
$bind=[
':rnd_id'=>$rnd_id
];

//prepare the statement
$statement = $link->prepare($query);
//execute the statement
$statement->execute($bind);

第二个 INSERT 使用 SELECT 从 product 表中获取产品价格。如您所见,我让 mysql 在查询中进行计算,因此无需在单独的代码中进行计算。

我只是告诉 mysql 使用 NOW() 使用当前日期和时间,而不是使用 $date$time

为什么我对数量折扣使用多个参数:遗憾的是,PDO 无法重复使用参数。所以如果你想在查询中再次使用它,你必须重新定义它。

$query = ' 
INSERT INTO order_details (
orderID,
orderType,
orderDate,
orderTime,
quantity,
orderPrice,
discount,
discountPrice,
employeeID
)
SELECT
:rnd_id ,
:type ,
NOW() ,
NOW() ,
:quant1 ,
(unitPrice * :quant2), //'unitPrice from table product, mysql does the calculation'
:disc1 ,
(unitPrice * :quant3) - ((unitPrice * :quant4) * :disc2) ,
:empID
FROM product WHERE productID = :prodID
';

$bind=[
':rnd_id' =>$rnd_id,
':type' =>$type,
':quant1' =>$quantity,
':quant2' =>$quantity,
':quant3' =>$quantity,
':quant4' =>$quantity,
':disc1' =>$dis,
':disc2' =>$dis,
':prodID' =>$pid,
':empID' =>$eid
];
$statement = $link->prepare($query);
$statement->execute($bind);

关于您的代码还有很多内容,但这涵盖了基础知识。我已经测试了查询(它们有效),但没有测试 PHP 代码。所以某处可能有错误。

关于php - 运行多个sql,需要先运行一个,然后两个一起运行 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55877418/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com