-2

i am implementing clothing shopping website in which admin manages stock for example add, update, delete stock. i am having problem with my delete query. stock is displayed in table and every row has its own delete button. when i click on delete button it always takes last row id and delete that and not that row which i want. Query is taking always last row id. CODE:

  <form action="" method="post" enctype="multipart/form-data" name="deleting" >
            <table align="center" border="0" id="myTable" class="table table-striped table-bordered table-list">
    <tr>
      <th>Product Code</th>
      <th>Brand</th>
      <th>Price</th>
      <th>Gender</th>
      <th>Category</th>
      <th>Material</th>
      <th>Size</th>
      <th>Description</th>
      <th>Quantity</th>
      <th>Delete Stock</th>
      </tr> 

<?php
$sql = "SELECT * FROM add_stock  ORDER BY id DESC"; 
                $rs_result = mysqli_query ($sql);
                   while ($result=mysqli_fetch_array($rs_result) )
            {
              ?>
              <tr>

                <td><?php echo $result['id'];?></td>
                <td><?php echo $result['brand_name'];?></td>
                <td><?php echo $result['price'];?></td>
                <td><?php echo $result['gender_name'];?></td>
                <td><?php echo $result['category_name'];?></td>
                <td><?php echo $result['material_name'];?></td>
                <td><?php echo $result['size_name']; ?></td>
                <td><?php echo $result['dress_description'];?></td>
                <td><?php echo $result['dress_quantity'];?></td>
               <td><input type="hidden" name="ID" value="<?php echo $result['id']; ?>"><input type="submit" name="delete" value="Delete" ></td>

              </tr>
              <?php
            }      
            ?>
</table>
</form>

  <?php 

if (isset($_POST['delete'])) {
$id=$_POST['ID']; //problem is here: it always takes last row id     
$link=mysqli_connect("localhost","root","") or die("Cannot Connect to the database!");
mysqli_select_db("login",$link) or die ("Cannot select the database!");
$query="DELETE FROM add_stock WHERE id='".$id."'";

$result=mysqli_query($query,$link) or die(mysqli_error($link));
if($result)
{ 
 echo '<script>confirm("Are you sure want to delete this record?")</script>';
 echo '<script>alert("Record ".$id." removed successfully!")</script>';
}
else
{
  die ("An unexpected error occured while <b>deleting</b> the record, Please try again!");

              }
              }
?>
  • You have taken form for delete only or other action also you have to perform for it? – B. Desai Aug 23 '17 at 10:36
  • i just use form for delete query @ B. Desai – Shoaib Sharoon Aug 23 '17 at 10:37
  • You must to open and close the form inside the while so each delete button have his own form – Nerea Aug 23 '17 at 10:39
  • Then try my code below @ShoaibSharoon – B. Desai Aug 23 '17 at 10:40
  • 2
    Hi @ShoaibSharoon: Hope you remember me. Till now, you have asked 11 questions and none of your question have any marked answer. And, I reminded you 1 week before regarding the same. But, still you have not marked anyone of it. See. People put their efforts to take you out of the problem. So, reward them by marking their answer as correct answer. – Nana Partykar Aug 23 '17 at 10:50
  • @ Nana Partykar ok i will mark. please check that cart popup question it still have problem – Shoaib Sharoon Aug 23 '17 at 10:57
  • 1
    yeah, you've asked that question 4 times... if you don't get an answer, most of the times the problem is the question itself; it's either not clear enough, or you've shown too less effort. I understand that you're new here, but please try to ask [high quality questions](https://stackoverflow.com/help/how-to-ask) and don't treat us as your private employees. – giorgio Aug 23 '17 at 10:59
  • 1
    No one is going to write full code for you. And, One more thing, I don't write answer just for reputation. I don't care you mark it or not. But, put your efforts to find where was the mistake. I already answered and you replied too by thanking. And, if it is not working after 4-5 days, then, it's not my issue. I'm not going to sit and check your code. You don't put your efforts to find the problem/issue @ShoaibSharoon – Nana Partykar Aug 23 '17 at 11:02

2 Answers2

2

I would rather suggest not to use <form> just for deleting purpose. You can use <a> tag to redirect it to other page for deleting purpose.

Here is my code.

index.php

<table align="center" border="0" id="myTable" class="table table-striped table-bordered table-list">
  <tr>
    <th>Product Code</th>
    <th>Brand</th>
    <th>Price</th>
    <th>Gender</th>
    <th>Category</th>
    <th>Material</th>
    <th>Size</th>
    <th>Description</th>
    <th>Quantity</th>
    <th>Delete Stock</th>
  </tr> 
  <?php
  $sql = "SELECT * FROM add_stock  ORDER BY id DESC";
  $rs_result = mysqli_query($sql);
  while ($result = mysqli_fetch_array($rs_result)) {?>
    <tr>
      <td><?php echo $result['id']; ?></td>
      <td><?php echo $result['brand_name']; ?></td>
      <td><?php echo $result['price']; ?></td>
      <td><?php echo $result['gender_name']; ?></td>
      <td><?php echo $result['category_name']; ?></td>
      <td><?php echo $result['material_name']; ?></td>
      <td><?php echo $result['size_name']; ?></td>
      <td><?php echo $result['dress_description']; ?></td>
      <td><?php echo $result['dress_quantity']; ?></td>
      <td>
        <a href="deleteStock.php?id=<?php echo $result['id'];?>">
          <input type="button" value="Delete" >
        </a>
      </td>
    </tr>
    <?php }?>
</table>

<?php
if(isset($_GET['delete'])){
  if($_GET['delete'] == "success"){
    echo '<script>alert("Record removed successfully!")</script>';
  }

  if($_GET['delete'] == "fail"){
    echo '<script>alert("An unexpected error occured while <b>deleting</b> the record, Please try again!")</script>';
  }
}

?>
<script>
$(document).on('click', "#myTable a", function(e) {
  if(confirm("Are you sure want to delete this record?")) {
    return true;
  } else {
    return false;
  }
});
</script>

deleteStock.php

<?php
if (isset($_GET['id'])) {
  $id = $_GET['id'];  
  $link = mysqli_connect("localhost", "root", "") or die("Cannot Connect to the database!");
  mysqli_select_db("login", $link) or die("Cannot select the database!");
  $query = "DELETE FROM add_stock WHERE id = $id";

  $result = mysqli_query($query, $link) or die(mysqli_error($link));

  if($result){
    header("location:index.php?delete=success");
  } else {
    header("location:index.php?delete=fail");
  }
}?>

[Important: And, still you have not created separate file for DB Connection, which I have already mentioned in my answer of your question modal popup keep populating only first item data on all item buttons 1 Week before. It implies, you don't learn from your mistake or you don't need any suggestions.]

Nana Partykar
  • 10,556
  • 10
  • 48
  • 77
1

Your script taking last row in post always because you have single form for all your data and your last value will be overwrite all previous data. Intead of it remove form from out side of table add add it to td where you have specify hidden field and delete button. Like below:

<table align="center" border="0" id="myTable" class="table table-striped table-bordered table-list">
    <tr>
      <th>Product Code</th>
      <th>Brand</th>
      <th>Price</th>
      <th>Gender</th>
      <th>Category</th>
      <th>Material</th>
      <th>Size</th>
      <th>Description</th>
      <th>Quantity</th>
      <th>Delete Stock</th>
      </tr> 

<?php
$sql = "SELECT * FROM add_stock  ORDER BY id DESC"; 
                $rs_result = mysqli_query ($sql);
                   while ($result=mysqli_fetch_array($rs_result) )
            {
              ?>
              <tr>

                <td><?php echo $result['id'];?></td>
                <td><?php echo $result['brand_name'];?></td>
                <td><?php echo $result['price'];?></td>
                <td><?php echo $result['gender_name'];?></td>
                <td><?php echo $result['category_name'];?></td>
                <td><?php echo $result['material_name'];?></td>
                <td><?php echo $result['size_name']; ?></td>
                <td><?php echo $result['dress_description'];?></td>
                <td><?php echo $result['dress_quantity'];?></td>
               <td><form action="" method="post" name="deleting" ><input type="hidden" name="ID" value="<?php echo $result['id']; ?>"><input type="submit" name="delete" value="Delete" ></form></td>

              </tr>
              <?php
            }      
            ?>
</table>

Also Remove enctype="multipart/form-data" from form its needed only if you have to upload file

B. Desai
  • 16,414
  • 5
  • 26
  • 47