php - PHP - 客户选择 X 数量 - 想要从库存和电子邮件中获取 X 数量
问题描述
所以我已经尝试了一段时间,似乎无法找到解决方案。
我有一个网站,销售数字商品。
当客户结帐时,它会询问数量。
我想这样当他们输入例如数量 5 时,我希望它从数据库中抓取 5 只股票并将所有股票发送到他们的电子邮件。
我需要让它循环并从assignedProduct 是产品ID 的库存表中获取x 数量。
目前,我的代码只获取数量而不是从我的库存数据库中获取不同的行。我如何让它循环股票?
我当前的代码是(使用区块链支付处理器):
<?php
$smtp_btc = $pdo->prepare('SELECT * FROM `productitems` WHERE `avaliable` = :avaliable AND `assignedProduct` = :assignedProduct');
$smtp_btc->execute(array(':avaliable' => '0', ':assignedProduct' => $product_id));
$query = $smtp_btc->fetchAll();
// Select all from stock where avaliable = true and assignedProduct is the main productID
// Then make it
// Foreach loop here
$i = 0;
$maxiterations = $quantity - 1;
$message = '';
foreach($query as $row_product) {
while($i <= $maxiterations) {
$i++;
$product_link_stock = $row_product['code'];
$stock_id_stock = $row_product['id'];
$message = "<tr style='background: #eee;'><td><strong>Email/Username:Pass(".$i."):</strong> </td><td>$product_link_stock</td></tr>";
echo $message.'<br>';
}
}
?>
解决方案
所以事实证明我需要在我的 while 循环内进行查询,以便它可以更改它为每个数量选择的行。
这是我的工作代码:
$i = 1;
$maxiterations = $quantity;
while($i <= $maxiterations) {
$smtp_btc = $pdo->prepare("SELECT * FROM `productitems` WHERE `avaliable` = :avaliable AND `assignedProduct` = :assignedProduct LIMIT $maxiterations");
$smtp_btc->execute(array(':avaliable' => '0', ':assignedProduct' => $product_id));
$query = $smtp_btc->fetchAll();
foreach($query as $row_product) {
$product_link_stock = $row_product['code'];
$stock_id_stock = $row_product['id'];
$message .= "<tr style='background: #eee;'><td><strong>Email/Username:Pass(".$i."):</strong> </td><td>$product_link_stock</td></tr>";
// Update stock foreach stock product they require (quantity depends on this)
$updateStock = $pdo->prepare('UPDATE `productitems` SET `avaliable` = :avaliable WHERE `id` = :id');
$updateStock->execute(array(':avaliable' => '1', ':id' => $stock_id_stock));
$i++;
}
$i++;
}
推荐阅读
- python - Pygame,pygame_menu - 启动后关闭和视频系统未初始化错误
- c++ - 如何在 C++ 中序列化/反序列化 INT
- c - 理解 linux 中的 membarier 函数
- c# - LinearGradientBrush 未按预期 WPF 工作
- javascript - 在 for 循环中使用 SetTimeOut
- cloud - 将数据保存到云端
- c# - 在 windows 窗体 c# 中输入行数和列数后,如何将嵌套的符号循环输出到标签?
- reactjs - Document.getElementsbyid().value 问题
- azure-devops - Azure devops 管道 CmdLine 任务脚本错误
- python - 从pdf中提取文本时如何忽略表格及其内容