简体   繁体   中英

Select * from table where colum = username not working

I'm trying to implement discount code on a site where the payment gateway is stipe, everything kinda working fine but when i try to get the % of the discount base on the promo code the user input from the database its not working, instead of charging the discounted price, it charge the full price and completely ignores the discount

here the code

$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'];

so instead of

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

its giving me

$amount = $total;

for some reason

You're not accumulating all the discounts. Each time through the loop you just apply the current row's discount to the total, overwriting what you calculated from the previous rows.

Initialize $grandtotal to the total, then subtract from it each time through.

Also, you shouldn't allow lots of discounts to let the grand total go below 0. I check for that at the end.

$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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM