计算错误PHP MYSQLI购物车

时间:2018-02-05 20:03:47

标签: php mysql mysqli cart shopping

我用PHP和Mysqli购物车在测试服务器www.petererikhansen.dk/bitcoin购买和销售加密货币,为每位用户提供10,000美元的起始资金 用户:test@test.com 密码:abc

当我下订单时,它会错误地计算余额(saldo)。例如,如果我以872.88美元购买1 x Bitcoincash BCH,我的余额应为9,127,12美元,但显示为8.254,12美元。

我有一个存储id,电子邮件,密码和saldo的数据库表。 Home.php(使用正确的用户名和密码输入的网站):

<?php
    $query = "SELECT * FROM bitcoin_products ORDER BY id ASC";
    $result = mysqli_query($conn, $query);
    ?>
    <div class="row">

    <?php
    if(mysqli_num_rows($result) > 0){
        while($row = mysqli_fetch_array($result)){
    ?>

    <div class="col-md-3">
        <form method="post" action="home.php?action=add&id=<?php echo $row["id"]; ?>">
            <div class="product">
                <img src="<?php echo $row["image"]; ?>" id="image">
                <h5 class="text-info"><?php echo $row["title"]; ?> <?php echo $row['symbol']; ?></h5>
                <h5 class="text-danger">$ <?php echo $row["price"]; ?></h5>
                <input type="text" name="quantity" class="form-control text-center" value="1">
                <input type="hidden" name="hidden_name" value="<?php echo $row["title"]; ?>">
                <input type="hidden" name="hidden_price" value="<?php echo $row["price"]; ?>">
                <input type="submit" name="add" style="margin-top: 5px;" class="btn btn-success btn-block" value="Køb">
            </div><!-- /product -->
        </form>
    </div><!-- /col-md-3 -->

    <?php
        }
    }
    ?>
    </div><!-- /row -->

    <div style="clear: both"></div>
    <br />
    <h3 class="title2">Mine ordrer</h3>

    <div class="table-responsive">
        <table class="table table-bordered">
        <tr>
            <th width="30%">Valuta</th>
            <th width="10%">Antal</th>
            <th width="13%">Beløb</th>
            <th width="10%">I alt</th>
            <th width="17%">Slet</th>
        </tr>

        <?php
        $total = 0;

        // get saldo as $saldo
        $sql = "SELECT saldo FROM bitcoin_users WHERE email = '$email'";
        $results = mysqli_query($conn, $sql);
        if(mysqli_num_rows($results) > 0){
            while($row = mysqli_fetch_array($results)){
                $saldo = $row['saldo'];
            }
        }

        if(!empty($_SESSION["cart"])){
                foreach($_SESSION["cart"] as $key => $value){
                    ?>
            <tr>
                <td><?php echo $value["item_name"]; ?></td>
                <td><?php echo $value["item_quantity"]; ?></td>
                <td>$ <?php echo $value["product_price"]; ?></td>
                <td>$ <?php echo number_format($value["item_quantity"] * $value["product_price"], 2); ?></td>
                <td><a href="home.php?action=delete&id=<?php echo $value["product_id"]; ?>"><span class="text-danger">Slet</span></a></td>
            </tr>
            <?php
                $total = $total + ($value["item_quantity"] * $value["product_price"]);
                $saldo = $saldo - ($value["item_quantity"] * $value["product_price"]);
                mysqli_query($conn, "UPDATE bitcoin_users SET saldo = '$saldo' WHERE email='$email' LIMIT 1");
                }
            ?>
            <tr>
                <td colspan="3" align="right">Total</td>
                <th align="right">$ <?php echo number_format($total, 2); ?></th>
                <td></td>
            </tr>
            <tr>
                <td colspan="3" align="right">Saldo</td>
                <th align="right">$ <?php echo number_format($saldo, 2); ?></th>
                <td></td>
            </tr>
            <?php   
                } else {
            ?>
            <tr>
                <td colspan="3" align="right">Total</td>
                <th align="right">$ <?php echo number_format($total, 2); ?></th>
                <td></td>
            </tr>
            <tr>
                <td colspan="3" align="right">Saldo</td>
                <th align="right">$ <?php echo number_format($saldo, 2); ?></th>
                <td></td>
            </tr>
            <?php
        }
        ?>
        </table>

我做错了什么? 提前谢谢!

0 个答案:

没有答案