Always the first line of the html table is removed using the php code

advertisements

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..


The problem is not in the cart delete, it is in order.php. Two things I see here.

First up, you are opening each form inside the while loop but you don't close the form until outside the loop (and you never closed the <tr>s). This results in:

<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>

Only the outer form is closed. That corresponds to the first row on the table. No matter which submit button you click in the table, they all map back to the first one.

Second, I suggest you give each form a unique id.

echo "<form id=\"addform" . echo $row['itemId'] . "\" method="post" action="cartdelete.php"> ';

I have had instances of multiple forms on a page with the same id giving different results in different browsers.

$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>";

There is also a problem in the cartdelete.php. This line:

$sql1="select itemId from cart";

will only find the very first item in the cart. No matter what, it will never find the second or third item. It might not even be needed. Since I don't know the table structure, try this:

  $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);