首页 > 解决方案 > 从表中选择 * 列 = 用户名不起作用

问题描述

我正在尝试在支付网关存在的网站上实施折扣代码,一切都很好,但是当我尝试根据促销代码获取折扣的百分比时,用户从数据库输入的内容不起作用,而不是收取折扣价,它收取全价,完全忽略折扣

这里的代码

$getcode = $getcode[1];
$user = $_SESSION['username'];
$total = $_SESSION['totprice'];

$sql6 = 'SELECT * from promocode where username="'.$user.'" and promo_code="'.$getcode.'" ';
$result6 = mysqli_query($con, $sql6);

while($row6 = mysqli_fetch_assoc($result6)) {
    echo "Discount: " . $row6["discount"]. "<br>";

    $discount =$row6["discount"];
    $grandtotal= $total - (($discount * $total) / 100);

    $publishable_key    = "*******************";
    $secret_key         = "********************";

    if(isset($_POST['stripeToken'])){


    Stripe::setApiKey($secret_key);
    $description    = "Invoice #".rand(99999,999999999) ;
    $amount     = $grandtotal;
    $user   = $_SESSION['username'];

所以而不是

$amount = $total - (($discount * $total) / 100);

它给了我

$amount = $total;

由于某些原因

标签: phpmysqli

解决方案


您没有累积所有折扣。每次循环时,您只需将当前行的折扣应用于总计,覆盖您从前几行计算的内容。

初始化$grandtotal为总数,然后每次都从中减去。

另外,你不应该允许很多折扣让总和低于 0。我在最后检查一下。

$grandtotal = $total;
while ($row6 = mysqli_fetch_assoc($result6)) {
    echo "Discount: " . $row6["discount"]. "<br>";
    $discount = $row6["discount"];
    $grandtotal -= $total - (($discount * $total) / 100);
}
if ($grandtotal < 0) {
    $grandtotal = 0;
}

推荐阅读