Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP购物车总价计算不正确_Php_Mysql_Shopping Cart - Fatal编程技术网

PHP购物车总价计算不正确

PHP购物车总价计算不正确,php,mysql,shopping-cart,Php,Mysql,Shopping Cart,这里有一个有趣的问题…我不知道问题出在哪里,因为我使用的代码来自另一个我拥有的站点。第一个网站工作完美,没有问题…但当我把它转换到我的新网站,总价格金额将无法正确计算 问题:当我切换代码时,是否遗漏了什么?如果我把下面的代码放到while循环中,它可以只处理一个项目,但不能处理多个项目,如果我将位置更改为这个,它看起来不正确 <div class="column text-lg">Subtotal: <span class="text-medium">$<?php

这里有一个有趣的问题…我不知道问题出在哪里,因为我使用的代码来自另一个我拥有的站点。第一个网站工作完美,没有问题…但当我把它转换到我的新网站,总价格金额将无法正确计算

问题:当我切换代码时,是否遗漏了什么?如果我把下面的代码放到while循环中,它可以只处理一个项目,但不能处理多个项目,如果我将位置更改为这个,它看起来不正确

<div class="column text-lg">Subtotal: <span class="text-medium">$<?php echo $totalamount; ?></span></div>
小计:$
以下是我的一些图片和代码:

旧工作代码:

<?php
if ( ! isset($totalamount)) {
$totalamount=0;
}
$totalquantity=0;
if (!session_id()) {
session_start();
}
include ('core/connectdb.php');
$sessid = session_id();
$query = "SELECT * FROM cart WHERE cart_sess = '$sessid'";
$results = mysqli_query($connect, $query) or die (mysql_query());
if(mysqli_num_rows($results)==0)
{
echo '<div id="content" class="float_r"><div align="center"><h3>Your cart is empty.</h3> You can find our items on our <a href="products.php">product page</a>.</div></div><div class="cleaner"></div>';
}
else
{
?>
<div id="content" class="float_r">
<div align="center"><h1>Shopping Cart</h1></div>
<table border="1" align="center" cellpadding="5">
<tr><td> Item Code</td><td>Quantity</td><td>Item Name</td><td>Price</
td><td>Total Price</td>
<?php
while ($row = mysqli_fetch_array($results, MYSQLI_ASSOC)) {
extract($row);
echo "<tr><td>";
echo $cart_itemcode;
echo "</td>";
echo "<td><form method=\"POST\" action=\"cart.php?action=change&icode=
$cart_itemcode\"><input type=\"text\" name=\"modified_quantity\" size=\"2\"
value=\"$cart_quantity\">";
echo "</td><td>";
echo $cart_item_name;
echo "</td><td>";
echo '$' . $cart_price . '';
echo "</td><td>";
$totalquantity = $totalquantity + $cart_quantity;
$totalprice = number_format($cart_price * $cart_quantity, 2);
$totalamount=$totalamount + ($cart_price * $cart_quantity);
echo '$' . $totalprice . '';
echo "</td><td>";
echo "<input type=\"submit\" name=\"Submit\" value=\"Change quantity\">
</form></td>";
echo "<td>";
echo "<form method=\"POST\" action=\"cart.php?action=delete&icode=$cart_itemcode\">";
echo "<input type=\"submit\" name=\"Submit\" value=\"Delete Item\"></form>
</td></tr>";
}
echo "<tr><td >Total</td><td>$totalquantity</td><td></td><td></td><td>";
$totalamount = number_format($totalamount, 2);
echo '$' . $totalamount . '';
echo "</td></tr>";
echo "</table><br>";
echo "<div style=\"width:400px; margin:auto;\">You currently have " .
$totalquantity . " product(s) selected in your cart</div> ";
?>
<table border="0" style="margin:auto;">
<tr>
<td><button style="font-family:verdana; font-size:150%;" onclick="goBack()">Go Back</button></td>
<td style="padding: 10px;">
<form method="POST" action="cart.php?action=empty">
<input type="submit" name="Submit" value="Empty Cart"
style="font-family:verdana; font-size:150%;" >
</form>
</td><td>
<?php include('cart_upload.php'); ?>
</td></tr></table>
</div>
<div class="cleaner"></div>
<?php
}
?>
<?php
if ( ! isset($totalamount)) {
$totalamount=0;
}
$totalquantity=0;
if (!session_id()) {
session_start();
}
include ('core/connectdb.php');
$sessid = session_id();
$query = "SELECT * FROM cart WHERE cart_sess = '$sessid'";
$results = mysqli_query($connect, $query) or die (mysql_query());
if(mysqli_num_rows($results)==0)
{
echo '<div"><div align="center"><h3>Your cart is empty.</h3> You can find our items on our <a href="products.php">product page</a>.</div></div>';
}
else
{
?>
    <!-- Page Title-->
      <div class="page-title">
        <div class="container">
          <div class="column">
            <h1>Cart</h1>
          </div>
          <div class="column">
            <ul class="breadcrumbs">
              <li><a href="index.php">Home</a>
              </li>
              <li class="separator">&nbsp;</li>
              <li>Cart</li>
            </ul>
          </div>
        </div>
      </div>
      <!-- Page Content-->
      <div class="container padding-bottom-3x mb-1">
        <!-- Shopping Cart-->
        <div class="table-responsive shopping-cart">
          <table class="table">
            <thead>
              <tr>
                <th>Product Name</th>
                <th class="text-center">Quantity</th>
                <th class="text-center">Subtotal</th>
                <th class="text-center"><a class="btn btn-sm btn-outline-danger" href="#">Clear Cart</a></th>
              </tr>
            </thead>
            <tdbody>
            <?php
            while ($row = mysqli_fetch_array($results, MYSQLI_ASSOC)) {
            extract($row);
            $cart_price = number_format($cart_price);
              echo '<tr>';
                echo '<td>';
                  echo '<div class="product-item"><a class="product-thumb" href="shop-single.php?item=' . $cart_itemcode . ' "><img src="' . $cart_imagename . '" alt="' . $cart_item_name . '"></a>';
                    echo '<div class="product-info">';
                      echo '<h4 class="product-title"><a href="shop-single.php?item=' . $cart_itemcode . ' ">' . $cart_item_name . '</a></h4>';
                    echo '</div>';
                  echo '</div>';
                echo '</td>';
                echo '<td class="text-center">';
                  echo '<div class="count-input">';
                  echo '$' . $cart_price . ' Each';
                      echo "<form method=\"POST\" action=\"cart.php?action=change&icode=
                            $cart_itemcode\"><input type=\"text\" name=\"modified_quantity\" size=\"2\"
                            value=\"$cart_quantity\"><br\><input type=\"submit\" name=\"Submit\" value=\"Update\">
                            </form>";
                  echo '</div>';
                echo '</td>';
                $totalquantity = $totalquantity + $cart_quantity;
                $totalprice = number_format($cart_price * $cart_quantity);
                $totalamount= number_format($totalamount + ($cart_price * $cart_quantity));
                echo '<td class="text-center text-lg text-medium">$' . $totalprice . '</td>';
                echo '<td class="text-center"><a class="remove-from-cart" href="cart.php?action=delete&icode=' . $cart_itemcode . '" data-toggle="tooltip" title="Remove item"><i class="icon-cross"></i></a></td>';
              echo '</tr>';
            }
}
?>
</tbody>
</table>
</div>
<div class="shopping-cart-footer">
<div class="column text-lg">Subtotal: <span class="text-medium">$<?php echo $totalamount; ?></span></div>
</div>
<div class="shopping-cart-footer">
<div class="column"><a class="btn btn-outline-secondary" onclick="goBack()"><i class="icon-arrow-left"></i>&nbsp;Back to Shopping</a></div>
<div class="column"><a class="btn btn-primary" href="#" data-toast data-toast-type="success" data-toast-position="topRight" data-toast-icon="icon-circle-check" data-toast-title="Your cart" data-toast-message="is updated successfully!">Update Cart</a><a class="btn btn-success" href="checkout-address.php">Checkout</a></div>
</div>
</div>

购物车
项目代码数量项目名称价格总价
回去
新的非工作代码:

<?php
if ( ! isset($totalamount)) {
$totalamount=0;
}
$totalquantity=0;
if (!session_id()) {
session_start();
}
include ('core/connectdb.php');
$sessid = session_id();
$query = "SELECT * FROM cart WHERE cart_sess = '$sessid'";
$results = mysqli_query($connect, $query) or die (mysql_query());
if(mysqli_num_rows($results)==0)
{
echo '<div id="content" class="float_r"><div align="center"><h3>Your cart is empty.</h3> You can find our items on our <a href="products.php">product page</a>.</div></div><div class="cleaner"></div>';
}
else
{
?>
<div id="content" class="float_r">
<div align="center"><h1>Shopping Cart</h1></div>
<table border="1" align="center" cellpadding="5">
<tr><td> Item Code</td><td>Quantity</td><td>Item Name</td><td>Price</
td><td>Total Price</td>
<?php
while ($row = mysqli_fetch_array($results, MYSQLI_ASSOC)) {
extract($row);
echo "<tr><td>";
echo $cart_itemcode;
echo "</td>";
echo "<td><form method=\"POST\" action=\"cart.php?action=change&icode=
$cart_itemcode\"><input type=\"text\" name=\"modified_quantity\" size=\"2\"
value=\"$cart_quantity\">";
echo "</td><td>";
echo $cart_item_name;
echo "</td><td>";
echo '$' . $cart_price . '';
echo "</td><td>";
$totalquantity = $totalquantity + $cart_quantity;
$totalprice = number_format($cart_price * $cart_quantity, 2);
$totalamount=$totalamount + ($cart_price * $cart_quantity);
echo '$' . $totalprice . '';
echo "</td><td>";
echo "<input type=\"submit\" name=\"Submit\" value=\"Change quantity\">
</form></td>";
echo "<td>";
echo "<form method=\"POST\" action=\"cart.php?action=delete&icode=$cart_itemcode\">";
echo "<input type=\"submit\" name=\"Submit\" value=\"Delete Item\"></form>
</td></tr>";
}
echo "<tr><td >Total</td><td>$totalquantity</td><td></td><td></td><td>";
$totalamount = number_format($totalamount, 2);
echo '$' . $totalamount . '';
echo "</td></tr>";
echo "</table><br>";
echo "<div style=\"width:400px; margin:auto;\">You currently have " .
$totalquantity . " product(s) selected in your cart</div> ";
?>
<table border="0" style="margin:auto;">
<tr>
<td><button style="font-family:verdana; font-size:150%;" onclick="goBack()">Go Back</button></td>
<td style="padding: 10px;">
<form method="POST" action="cart.php?action=empty">
<input type="submit" name="Submit" value="Empty Cart"
style="font-family:verdana; font-size:150%;" >
</form>
</td><td>
<?php include('cart_upload.php'); ?>
</td></tr></table>
</div>
<div class="cleaner"></div>
<?php
}
?>
<?php
if ( ! isset($totalamount)) {
$totalamount=0;
}
$totalquantity=0;
if (!session_id()) {
session_start();
}
include ('core/connectdb.php');
$sessid = session_id();
$query = "SELECT * FROM cart WHERE cart_sess = '$sessid'";
$results = mysqli_query($connect, $query) or die (mysql_query());
if(mysqli_num_rows($results)==0)
{
echo '<div"><div align="center"><h3>Your cart is empty.</h3> You can find our items on our <a href="products.php">product page</a>.</div></div>';
}
else
{
?>
    <!-- Page Title-->
      <div class="page-title">
        <div class="container">
          <div class="column">
            <h1>Cart</h1>
          </div>
          <div class="column">
            <ul class="breadcrumbs">
              <li><a href="index.php">Home</a>
              </li>
              <li class="separator">&nbsp;</li>
              <li>Cart</li>
            </ul>
          </div>
        </div>
      </div>
      <!-- Page Content-->
      <div class="container padding-bottom-3x mb-1">
        <!-- Shopping Cart-->
        <div class="table-responsive shopping-cart">
          <table class="table">
            <thead>
              <tr>
                <th>Product Name</th>
                <th class="text-center">Quantity</th>
                <th class="text-center">Subtotal</th>
                <th class="text-center"><a class="btn btn-sm btn-outline-danger" href="#">Clear Cart</a></th>
              </tr>
            </thead>
            <tdbody>
            <?php
            while ($row = mysqli_fetch_array($results, MYSQLI_ASSOC)) {
            extract($row);
            $cart_price = number_format($cart_price);
              echo '<tr>';
                echo '<td>';
                  echo '<div class="product-item"><a class="product-thumb" href="shop-single.php?item=' . $cart_itemcode . ' "><img src="' . $cart_imagename . '" alt="' . $cart_item_name . '"></a>';
                    echo '<div class="product-info">';
                      echo '<h4 class="product-title"><a href="shop-single.php?item=' . $cart_itemcode . ' ">' . $cart_item_name . '</a></h4>';
                    echo '</div>';
                  echo '</div>';
                echo '</td>';
                echo '<td class="text-center">';
                  echo '<div class="count-input">';
                  echo '$' . $cart_price . ' Each';
                      echo "<form method=\"POST\" action=\"cart.php?action=change&icode=
                            $cart_itemcode\"><input type=\"text\" name=\"modified_quantity\" size=\"2\"
                            value=\"$cart_quantity\"><br\><input type=\"submit\" name=\"Submit\" value=\"Update\">
                            </form>";
                  echo '</div>';
                echo '</td>';
                $totalquantity = $totalquantity + $cart_quantity;
                $totalprice = number_format($cart_price * $cart_quantity);
                $totalamount= number_format($totalamount + ($cart_price * $cart_quantity));
                echo '<td class="text-center text-lg text-medium">$' . $totalprice . '</td>';
                echo '<td class="text-center"><a class="remove-from-cart" href="cart.php?action=delete&icode=' . $cart_itemcode . '" data-toggle="tooltip" title="Remove item"><i class="icon-cross"></i></a></td>';
              echo '</tr>';
            }
}
?>
</tbody>
</table>
</div>
<div class="shopping-cart-footer">
<div class="column text-lg">Subtotal: <span class="text-medium">$<?php echo $totalamount; ?></span></div>
</div>
<div class="shopping-cart-footer">
<div class="column"><a class="btn btn-outline-secondary" onclick="goBack()"><i class="icon-arrow-left"></i>&nbsp;Back to Shopping</a></div>
<div class="column"><a class="btn btn-primary" href="#" data-toast data-toast-type="success" data-toast-position="topRight" data-toast-icon="icon-circle-check" data-toast-title="Your cart" data-toast-message="is updated successfully!">Update Cart</a><a class="btn btn-success" href="checkout-address.php">Checkout</a></div>
</div>
</div>

运货马车
  • 推车
品名 量 小计 小计:$
看起来您正在将格式化的数字相加,这肯定会带来麻烦。不要那样做。在内部尽可能保持原始值,只有在向用户显示时才进行格式化:

$totalquantity = $totalquantity + $cart_quantity;
$totalprice = $cart_price * $cart_quantity;
$totalamount = $totalamount + ($cart_price * $cart_quantity);

echo '<td class="text-center text-lg text-medium">$' . number_format($totalprice) . '</td>';
$totalquantity=$totalquantity+$cart\u数量;
$totalprice=$cart\u price*$cart\u数量;
$totalamount=$totalamount+($cart\u price*$cart\u quantity);
回显“$”。数字格式($totalprice)。“”;

记住,乱七八糟的代码是bug隐藏的地方。保持东西干净,就像你在厨房里工作一样。总是,总是让事情有条理。如果你被一段代码卡住了,并且不确定它为什么能工作,那么你应该做的第一件事就是清理它。然后继续打扫。有时在重新组织的过程中,错误变得很明显。

因此我发现了问题……在我的页面顶部,我有以下代码:

if ( ! isset($totalamount)) {
  $totalamount=0;
}
显然出于某种原因,代码认为它的值为2

一旦我在while循环之前向底部添加了以下内容:

$totalamount=0;

就像魔术一样,即使是格式化的数字,它现在也能正常工作。我想保留格式化的数字,因为我不希望价格中有任何美分,只要整美元。

注意:
mysqli
的面向对象接口明显不那么冗长,使代码更易于阅读和审核,并且不容易与过时的
mysql\u查询
接口混淆。在你对程序性风格投入太多之前,值得换一种。示例:
$db=newmysqli(…)
$db->prepare(“…”)
过程接口是PHP 4时代的产物,当时引入了
mysqli
API,不应在新代码中使用。
mysql\u error
是此处使用的错误函数。警告:使用
mysqli
时,应使用和向查询中添加用户数据。不要使用字符串插值或连接如果有人试图利用你的错误,这可能是非常有害的。很多问题都可以被发现并解决,因此错误不容易被忽略。这段代码的形状非常粗糙,它是一个复杂的过程HTML、CSS、SQL和PHP。为什么不使用一个经过验证的平台来构建,或者至少是类似的平台呢?非常感谢大家提供的相关信息……我非常感谢,并将尽我所能实现您提到的所有内容。我对安全代码还是相当陌生的,并且正在处理在线找到的代码片段。(是的,我知道这不是最安全的做法。)查询中从未使用过重要的用户信息,仅在注册过程中收集。我这边的结帐系统中从未使用过卡信息、地址等,仅通过PayPal使用。之所以编码如此混乱,是因为它是我的工作和其他工作的混合。稍后将进行清理。谢谢tadman,我将e清理代码,看看是否有什么我可以做的。