order.php
require_once(conn.php);
session_start();
$itemId=$_SESSION['itemId'];
$tnumber=$_SESSION['tnumber'];
$custno=$_SESSION['custno'];
$sql="select itemId,subtitle,price,quantity from cart where tnumber='$tnumber'" ;
$res=mysqli_query($dbhandle,$sql);
$total=0;
?>
<html>
<head>
<title>Home</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" type='text/css' href='cartbox.css'/>
</head>
<body>
<h1></h1>
<script src="home.js"></script>
<div id="shopping-cart"> </div>
<?php
echo "<table id='t1' border='1'>
<th>Subtitle</th>
<th>Quantity</th>
<th>Price</th>
<th>Amount</th>
</tr>";
while ($row=mysqli_fetch_array($res))
{
$amount=$row['price']*$row['quantity'];
echo "<tr>";
echo "<td>" .$row['subtitle'] ."</td>";
echo "<td>" .$row['quantity'] ."</td>";
echo "<td>" .$row['price'] ."</td>";
echo "<td>" . $amount . "</td>";
echo' <form id="addform" method="post" action="cartdelete.php"> ';
echo "<td><input type='submit' value='x' name='submit'></td>";
echo "<td><input type='text' name='itemId' value= ".$row['itemId']."></td>";
$total = $total+ $amount;
}
echo"</form>";
echo "</table>";
?>
<?php echo $total ?>
<input type="button" name="continue" value="Continue Order" style="position: absolute;top:200px;" onclick="location.href='customerdicecream.php'">
</body>
</html>
cartdelete.php
<?php
require_once(conn.php);
$itemId=(filter_input(\INPUT_POST,'itemId'));
$sql1="select itemId from cart";
$res1=mysqli_query($dbhandle,$sql1);
$row=mysqli_fetch_array($res1);
$itemId1=$row['itemId'];
if(!empty(\filter_input(INPUT_POST,'submit')))
{
$sql="delete from cart where itemId='$itemId1'";
$res=mysqli_query($dbhandle,$sql) or die(\mysqli_error($dbhandle));
}
echo"deleted successfully";
$mysqli_close = \mysqli_close($dbhandle);
我从购物车表中检索了价值并显示在html表格中,每行都有一个删除按钮。但每当我尝试删除而不管按钮时,只有表格的第一行被删除。我猜deletecart.php中的sql查询是错误的。请更正代码和查询..
答案 0 :(得分:1)
取决于,如果您尝试删除一行,那么这很好:
$sql="delete from cart where itemId='$itemId1'";
然而,要简单地删除所有内容:
$ sql =&#34; DELETE *来自购物车&#34;;
答案 1 :(得分:0)
问题不在购物车删除中,而是在order.php中。我在这里看到两件事。
首先,你在while循环中打开每个表单,但是你不要关闭表单直到循环外(并且你从未关闭&lt; tr&gt; s)。这导致:
<form id="addform" method="post" action="cartdelete.php">
....
<form id="addform" method="post" action="cartdelete.php">
....
<form id="addform" method="post" action="cartdelete.php">
....
</form>
仅关闭外部表单。这对应于表格的第一行。无论您在表格中单击哪个提交按钮,它们都会映射回第一个。
其次,我建议你给每个表单一个唯一的id。
echo "<form id=\"addform" . echo $row['itemId'] . "\" method="post" action="cartdelete.php"> ';
我在页面上有多个表单的实例,其ID相同,在不同的浏览器中显示不同的结果。
$total = 0;
while ($row=mysqli_fetch_array($res))
{
$amount=$row['price']*$row['quantity'];
echo "<tr>";
echo "<td>" .$row['subtitle'] ."</td>";
echo "<td>" .$row['quantity'] ."</td>";
echo "<td>" .$row['price'] ."</td>";
echo "<td>" . $amount . "</td>";
echo "<form id='addform{$row['itemId']}' method='post' action='cartdelete.php'> ";
echo "<td><input type='submit' value='x' name='submit'></td>";
echo "<td><input type='text' name='itemId' value= '{$row['itemId']}'></td>";
echo"</form>";
echo "</tr>";
$total += $amount;
}
echo "</table>";
cartdelete.php中也存在问题。这一行:
$sql1="select itemId from cart";
只能找到购物车中的第一件商品。无论如何,它永远不会找到第二或第三项。甚至可能不需要它。由于我不知道表结构,请尝试:
$itemId=(filter_input(\INPUT_POST,'itemId'));
if(!empty(\filter_input(INPUT_POST,'submit')))
{
$sql="delete from cart where itemId='$itemId'";
$res=mysqli_query($dbhandle,$sql) or die(\mysqli_error($dbhandle));
}
echo"deleted successfully";
$mysqli_close = \mysqli_close($dbhandle);