php - 总是使用 php 代码删除 html 表的第一行

标签 php mysql

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/

相关文章:

返回多个记录集的 MySQL 存储过程

mysql - SQL GROUP BY 和 MIN 查找每个部门中工资最低的人

mysql - 我应该对许多分类行使用单个表吗?

MySQL 查询以检索完整的 URL slug

php - Splstack 是如何工作的?为什么?

php - 上传脚本 mkdir 在同一目录时说文件已存在,即使文件名不同

php - 没有从查询中获取数据。 PDO

php - 使用jquery和php从mysql数据库获取信息

php - Apache 使用 https 全部重定向到 index.php

php - 是否可以将 php 代码插入 ' header("Location : example. php") ' 命令?