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);
i have retrieved the value from cart table and displayed in the html table,each row havhaving a button to delete.But whenever i try to delete irrespective of the button,only the first row of the table gets deleted.I guess the sql query in the deletecart.php is wrong.Please correct the code and the query..
最佳答案
视情况而定,如果您尝试删除一行,那么这很好:
$sql="delete from cart where itemId='$itemId1'";
但是要删除所有它很简单:
$sql="从购物车中删除 *";
关于php - 总是使用 php 代码删除 html 表的第一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23114355/