如何根据数量更改每个产品价值?

时间:2017-05-06 11:30:01

标签: php html

我创建了一个选择(列表/菜单)来更改产品的数量,并添加了一个javascript,以便在选择时更改。数据库和数量列中的所有产品的值都会更改,但页面上只有价格列上的第一个产品值更改,并且由于某种原因,这取决于应用于其他产品的第一个产品的数量。

举例说明我的观点: 产品1价格= 2
产品2价格= 3

如果我将产品1的数量更改为' 2',我会得到: 产品1价格= 4 即使产品2的数量设置为不同的数量,产品2的价格= 6。这个问题似乎错了,但我希望说明清楚。我添加了整个代码,因为我不确定问题究竟在哪里。我希望阅读清楚。

<?php require_once('Connections/MJ_conn.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
  $updateSQL = sprintf("UPDATE cart SET quantity=%s WHERE `counter`=%s",
                       GetSQLValueString($_POST['quantity'], "int"),
                       GetSQLValueString($_POST['counter'], "int"));

  mysql_select_db($database_MJ_conn, $MJ_conn);
  $Result1 = mysql_query($updateSQL, $MJ_conn) or die(mysql_error());

  $updateGoTo = "cart_list.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
    $updateGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $updateGoTo));
}

$varuser_CartData = "0";
if (isset($_SESSION["MM_UserID"])) {
  $varuser_CartData = $_SESSION["MM_UserID"];
}
mysql_select_db($database_MJ_conn, $MJ_conn);
$query_CartData = sprintf("SELECT * FROM cart WHERE cart.UserID=%s  AND cart.transactioncomplete = 0", GetSQLValueString($varuser_CartData, "int"));
$CartData = mysql_query($query_CartData, $MJ_conn) or die(mysql_error());
$row_CartData = mysql_fetch_assoc($CartData);
$totalRows_CartData = mysql_num_rows($CartData);
?>
<!DOCTYPE html>
<html>
<head>
    <title>Cart List</title>
    <script src="https://code.jquery.com/jquery-3.2.1.js"
     integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE="
     crossorigin="anonymous"></script>
    <meta charset="utf-8"/>
    <link rel="stylesheet" href="IndexStyle.css"/>
    <meta name="viewport" content="width= device-width, intial-scale=1.0">
</head>
<body class="body">
    <header class="MainHeader">
    <img src="images/header-image-5.jpg">
    <nav><ul>
         <li class="active"><a href="index.php">Home</a></li>
         <li><a href="Product.php">Products</a></li>
         <li><a href="AboutUs.php">About Us</a></li>
         <li><a href="ContactUs.php">Contact Us</a></li>
        </ul>
    </nav>
    </header>
    <div class="MainBody">
      <h1>Cart</h1>

      <table width="600" border="0">
        <tr>
          <td bgcolor="#CF5C3F">Product</td>
          <td bgcolor="#CF5C3F">Quantity</td>
          <td bgcolor="#CF5C3F">Price</td>
          <td bgcolor="#CF5C3F">Actions</td>
        </tr>
<?php         $totalprice = 0;
$quantity = $row_CartData['quantity'];?>        
<?php do { ?>
  <tr>
    <td><?php echo ObtainProductName($row_CartData['ProductID']); ?></td>
    <td><form name="form1" method="POST" action="<?php echo $editFormAction; ?>">
      <div align="center">
        <label for="quantity"></label>
        <select name="quantity" id="quantity" title="<?php echo $quantity ?>" onchange="this.form.submit();">
          <option value="1" selected <?php if (!(strcmp(1, $row_CartData['quantity']))) {echo "selected=\"selected\"";} ?>>1</option>
          <option value="2" <?php if (!(strcmp(2, $row_CartData['quantity']))) {echo "selected=\"selected\"";} ?>>2</option>
          <option value="3" <?php if (!(strcmp(3, $row_CartData['quantity']))) {echo "selected=\"selected\"";} ?>>3</option>
          <option value="4" <?php if (!(strcmp(4, $row_CartData['quantity']))) {echo "selected=\"selected\"";} ?>>4</option>
          <option value="5" <?php if (!(strcmp(5, $row_CartData['quantity']))) {echo "selected=\"selected\"";} ?>>5</option>
        </select>
        <input name="counter" type="hidden" id="counter" value="<?php echo $row_CartData['counter']; ?>">
      </div>
      <input type="hidden" name="MM_update" value="form1">
    </form></td>
    <td>RM<?php echo ObtainProductPrice($row_CartData['ProductID']) * $quantity ?></td>
    <td><a href="cart_delete.php?recordID=<?php echo $row_CartData['counter']; ?>">Delete</a></td>
  </tr>
    <?php 
    $totalprice = $totalprice + (ObtainProductPrice($row_CartData['ProductID']) * $quantity);?>
  <?php } while ($row_CartData = mysql_fetch_assoc($CartData)); ?>  
  <tr>
    <td></td>
    <td><div align="right"><strong>SubTotal</strong>:</div></td>
    <td>RM <?php echo $totalprice ?></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><div align="right"><strong>Tax:</strong></div></td>
    <td><?php echo ObtainTax(); ?>%</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><div align="right"><strong>Tax amount:</strong></div></td>
    <td>RM<?php 
    $taxRate= ObtainTax();
    $tax=$totalprice*$taxRate/100;
    $totalafter=$totalprice+$tax;
    echo $tax ?></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><div align="right"><strong>Total after Tax:</strong></div></td>
    <td>RM
      <?php 
    $taxRate= ObtainTax();
    $tax=$totalprice*$taxRate/100;
    $totalafter=$totalprice+$tax;
    echo $totalafter
     ?></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><form name="form2" method="post" action="">
      <div align="center">
        <input type="submit" name="updatecart" id="updatecart" value="Update Cart">
      </div>
    </form></td>
    <td><form name="form3" method="post" action="">
      <input type="submit" name="checkout" id="checkout" value="Checkout">
    </form></td>
    <td>&nbsp;</td>
  </tr>
      </table>
      <h2><a href="cart_payment_method.php">Choose the payment method</a></h2>
    </div>
    <aside class="SideBar">
<?php include ("/includes/catalogue.php"); ?>
    </aside>
    <Footer class="Footer"> 
        <p><strong>M&amp;J 2017 ©</strong></p>

    </Footer>
</body>
</html>
<?php
mysql_free_result($CartData);
?>

0 个答案:

没有答案