cart.php returns 只需要最后一个产品的值
cart.php returns wanted values just for the last product
当我点击 'Update' 时,它现在应该 return 给我一个产品 ID 和想要的数量,但最后一个结果只有 return产品。
当尝试对购物车中的其他产品执行此操作时,它只是 return 一个空引号(用于数量)和一个 ID 号。
这是我的代码。 cart.php
<?php
include("functions/functions.php");
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.0.12/css/all.css" integrity="sha384-G0fIWCsCzJIMAVNQPfjH08cyYaUtMwjJwqiRKxxE/rx96Uroj1BtIQ6MLJuheaO9" crossorigin="anonymous">
<link type="text/css" rel="stylesheet" href="styles/style.css" />
<title>Prodavnica+</title>
</head>
<body>
<div id="header" class="cf">
<a href="index.php"><img src="images/logo.png" /></a>
<div id="navbar">
<ul>
<li>
<a href="index.php"> Home</a>
</li>
<li>
<a href="all_products.php"> Products</a>
</li>
<li>
<a href="customer/my_account.php"> My Account</a>
</li>
<li>
<a href="#"> Sign Up</a>
</li>
<li>
<a href="cart.php"> Shopping Card</a>
</li>
<li>
<a href="#"> Contact Us</a>
</li>
</ul>
</div> <!-- END navbar -->
<div id="search">
<form method="get" action="results.php" enctype="multipart/form-data">
<input type="text" name="search_query" placeholder="Search Product" />
<input type="submit" name="search_button" value="Search" />
</form>
</div>
</div> <!-- END header -->
<?php cart(); ?>
<div id="shop-bar">
<p>
Total items: <?php totalItems() ?>
</p>
<p>
Total price: <?php totalPrice()?>
</p>
<a href="cart.php"><i class="fas fa-shopping-cart"> | </i></a>
<span> Welcome Guest! </span>
</div> <!-- END shop-bar -->
<div id="container">
<div id="main">
<div id="product-box-cart">
<form action="" method="post" enctype="multipart/form-data">
<table>
<thead>
<th>Product</th>
<th>Quantity</th>
<th>Remove</th>
<th>Price</th>
</thead>
<tbody>
<?php
$total = 0;
global $con;
$ip = getIp();
$run_price = mysqli_query($con,"SELECT * FROM cart WHERE ip_add = '$ip'");
while($row_pro_price = mysqli_fetch_array($run_price)) {
$pro_id = $row_pro_price['p_id'];
$pro_qty = $row_pro_price['qty'];
$run_pro_price2 = mysqli_query($con,"SELECT * FROM products WHERE product_id = '$pro_id'") or die(mysqli_error($con));
while($row_pro_price2 = mysqli_fetch_array($run_pro_price2)) {
$pro_price = array($row_pro_price2['product_price']);
$pro_title = $row_pro_price2['product_title'];
$product_image = $row_pro_price2['product_image'];
$single_price = $row_pro_price2['product_price'];
$pro_price_values = array_sum($pro_price);
$total += $pro_price_values;
?>
<tr>
<td>
<h2><?php echo $pro_title ?></h2>
<img src="admin/product_images/<?php echo $product_image;?>">
</td>
<td>
<input type="text" name="qty" placeholder="<?php echo $pro_qty; ?>">
<button type="submit" name="qty_btn" value = "<?php echo $pro_id; ?>"> Update </button>
</td>
<td>
<button type="submit" name="remove" value="<?php echo $pro_id; ?>"> Remove </button>
</td>
<td>
<?php echo "$" . $single_price; ?>
</td>
</tr>
</tbody>
<?php }} ?>
</table>
<p>
<b> Total Value: </b> <?php echo "$" . $total;?>
</p>
<div id="check-buttons">
<input type="submit" name="continue" value="Continue Shopping" />
<a href="checkout.php"><input type="button" value="Checkout" /></a>
</div> <!-- end cHECK BUTONS -->
</form>
<?php
if(isset($_POST['qty_btn'])) {
$qty = $_POST['qty'];
$qty_id = $_POST['qty_btn'];
printf($_POST['qty_btn']);
printf($_POST['qty']);
}elseif(!isset($_POST['qty_btn'])) {
if(isset($_POST['remove'])) {
$remove_id = $_POST['remove'];
var_dump($remove_id);
}
}
?>
</div> <!-- END product box -->
</div> <!-- END main -->
<div id="side">
<div id="side-category">
<h2>Categories</h2>
<hr />
<table id="mw">
<tr>
<?php
getBrands();
?>
</tr>
</table>
<ul>
<?php
getCats();
?>
</ul>
</div><!-- END side-category-->
</div> <!-- END side -->
</div> <!--END container -->
<div id="footer">
<p>© 2018 by Djordje Stamenkovic</p>
</div> <!-- END footer -->
</body>
</html>
昨天我针对同一个 cart.php 发布了一个类似的问题,其中我对整个购物车只有一个按钮,但没有任何解决方案,尝试了一种不同的方法,它让我得到了这个。
当您有一个 form
时 - 所有定义的输入都将发送到下一页,并且可以通过它们的 name
.
访问
如果您现在定义更多具有相同名称的字段 - 您这样做是因为您遍历不同的产品 - 您在提交后无法访问正确的值
你的结构
<form action>
while (...) {
<input name="qty" value="...">
<button submit>
}
</form>
所以你定义了多个同名input
qty
最好用一个输入名称="qty"定义多个form
。每个产品一个表格
while (...) {
<form action>
<input name="qty" value="...">
<button submit>
</form>
}
当我点击 'Update' 时,它现在应该 return 给我一个产品 ID 和想要的数量,但最后一个结果只有 return产品。 当尝试对购物车中的其他产品执行此操作时,它只是 return 一个空引号(用于数量)和一个 ID 号。
这是我的代码。 cart.php
<?php
include("functions/functions.php");
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.0.12/css/all.css" integrity="sha384-G0fIWCsCzJIMAVNQPfjH08cyYaUtMwjJwqiRKxxE/rx96Uroj1BtIQ6MLJuheaO9" crossorigin="anonymous">
<link type="text/css" rel="stylesheet" href="styles/style.css" />
<title>Prodavnica+</title>
</head>
<body>
<div id="header" class="cf">
<a href="index.php"><img src="images/logo.png" /></a>
<div id="navbar">
<ul>
<li>
<a href="index.php"> Home</a>
</li>
<li>
<a href="all_products.php"> Products</a>
</li>
<li>
<a href="customer/my_account.php"> My Account</a>
</li>
<li>
<a href="#"> Sign Up</a>
</li>
<li>
<a href="cart.php"> Shopping Card</a>
</li>
<li>
<a href="#"> Contact Us</a>
</li>
</ul>
</div> <!-- END navbar -->
<div id="search">
<form method="get" action="results.php" enctype="multipart/form-data">
<input type="text" name="search_query" placeholder="Search Product" />
<input type="submit" name="search_button" value="Search" />
</form>
</div>
</div> <!-- END header -->
<?php cart(); ?>
<div id="shop-bar">
<p>
Total items: <?php totalItems() ?>
</p>
<p>
Total price: <?php totalPrice()?>
</p>
<a href="cart.php"><i class="fas fa-shopping-cart"> | </i></a>
<span> Welcome Guest! </span>
</div> <!-- END shop-bar -->
<div id="container">
<div id="main">
<div id="product-box-cart">
<form action="" method="post" enctype="multipart/form-data">
<table>
<thead>
<th>Product</th>
<th>Quantity</th>
<th>Remove</th>
<th>Price</th>
</thead>
<tbody>
<?php
$total = 0;
global $con;
$ip = getIp();
$run_price = mysqli_query($con,"SELECT * FROM cart WHERE ip_add = '$ip'");
while($row_pro_price = mysqli_fetch_array($run_price)) {
$pro_id = $row_pro_price['p_id'];
$pro_qty = $row_pro_price['qty'];
$run_pro_price2 = mysqli_query($con,"SELECT * FROM products WHERE product_id = '$pro_id'") or die(mysqli_error($con));
while($row_pro_price2 = mysqli_fetch_array($run_pro_price2)) {
$pro_price = array($row_pro_price2['product_price']);
$pro_title = $row_pro_price2['product_title'];
$product_image = $row_pro_price2['product_image'];
$single_price = $row_pro_price2['product_price'];
$pro_price_values = array_sum($pro_price);
$total += $pro_price_values;
?>
<tr>
<td>
<h2><?php echo $pro_title ?></h2>
<img src="admin/product_images/<?php echo $product_image;?>">
</td>
<td>
<input type="text" name="qty" placeholder="<?php echo $pro_qty; ?>">
<button type="submit" name="qty_btn" value = "<?php echo $pro_id; ?>"> Update </button>
</td>
<td>
<button type="submit" name="remove" value="<?php echo $pro_id; ?>"> Remove </button>
</td>
<td>
<?php echo "$" . $single_price; ?>
</td>
</tr>
</tbody>
<?php }} ?>
</table>
<p>
<b> Total Value: </b> <?php echo "$" . $total;?>
</p>
<div id="check-buttons">
<input type="submit" name="continue" value="Continue Shopping" />
<a href="checkout.php"><input type="button" value="Checkout" /></a>
</div> <!-- end cHECK BUTONS -->
</form>
<?php
if(isset($_POST['qty_btn'])) {
$qty = $_POST['qty'];
$qty_id = $_POST['qty_btn'];
printf($_POST['qty_btn']);
printf($_POST['qty']);
}elseif(!isset($_POST['qty_btn'])) {
if(isset($_POST['remove'])) {
$remove_id = $_POST['remove'];
var_dump($remove_id);
}
}
?>
</div> <!-- END product box -->
</div> <!-- END main -->
<div id="side">
<div id="side-category">
<h2>Categories</h2>
<hr />
<table id="mw">
<tr>
<?php
getBrands();
?>
</tr>
</table>
<ul>
<?php
getCats();
?>
</ul>
</div><!-- END side-category-->
</div> <!-- END side -->
</div> <!--END container -->
<div id="footer">
<p>© 2018 by Djordje Stamenkovic</p>
</div> <!-- END footer -->
</body>
</html>
昨天我针对同一个 cart.php 发布了一个类似的问题,其中我对整个购物车只有一个按钮,但没有任何解决方案,尝试了一种不同的方法,它让我得到了这个。
当您有一个 form
时 - 所有定义的输入都将发送到下一页,并且可以通过它们的 name
.
如果您现在定义更多具有相同名称的字段 - 您这样做是因为您遍历不同的产品 - 您在提交后无法访问正确的值
你的结构
<form action>
while (...) {
<input name="qty" value="...">
<button submit>
}
</form>
所以你定义了多个同名input
qty
最好用一个输入名称="qty"定义多个form
。每个产品一个表格
while (...) {
<form action>
<input name="qty" value="...">
<button submit>
</form>
}