im trying to delete from a datatable when i click the remove button the data will be removed for a quick action in the admin side but my code dose not work i tried to fix it but i don’t see any problem in the code here is my code
<div class="table-responsive"> <table id="datas" class="table table-striped table-bordered fixed" style="width:100%"> <thead style="color:black;" > <th>id</th> <th>Company Name</th> <th>Product Name</th> <th>Weight</th> <th>Price Per Gram</th> <th>Quantity</th> <th>Type</th> <th>Category</th> <th>Product Price</th> <th>Image</th> <th>Total Price</th> <th class="text-center">Actions</th> </thead> <?php $get = mysqli_query($conn,"SELECT * FROM stock;"); ?> <tbody> <?php while ($row=mysqli_fetch_array($get)) { $id=$row['id']; $company=$row['company_name']; $name=$row['product_name']; $weight=$row['weight']; $price_per_gram=$row['price_per_gram']; $quantity=$row['quantity']; $type=$row['type']; $category=$row['category']; $price=$row['product_price']; $img=$row['img']; $total=$row['total_price']; ?> <tr> <td ><?php echo $id;?></td> <td><?php echo $company;?></td> <td><?php echo $name;?></td> <td><?php echo $weight;?> g</td> <td><?php echo $price_per_gram;?> $</td> <td><?php echo $quantity;?></td> <td><?php echo $type;?></td> <td><?php echo $category;?></td> <td><?php echo $price;?></td> <td> <img src="product_img/<?php echo $img; ?>" style="height:5rem;width:5rem;border-radius:10px;"> </td> <td><?php echo $total;?></td> <td style="width: 20px"> <a href="editstock.php?id=<?php echo $id;?>" class="btn btn-gradient-primary">Edit</a> <button class="btn btn-danger" name="delete" type="submit" value="<?php echo "$id" ?>">Delete</button> </td> </tr> <?php } ?> </tbody> </table> <?php if (isset($_POST['delete'])) { $delete = mysqli_query($conn,"DELETE FROM stock WHERE id= ".$_POST['delete']." "); header("location:viewstcok.php"); } ?>
i give the delete button a value when clicked so it will delete with that value but it did not seems to work can any one help me
Advertisement
Answer
you have to placed table inside <form></form>
tag
<div class="table-responsive"> <form> <table id="datas" class="table table-striped table-bordered fixed" style="width:100%"> <thead style="color:black;" > <th>id</th> <th>Company Name</th> <th>Product Name</th> <th>Weight</th> <th>Price Per Gram</th> <th>Quantity</th> <th>Type</th> <th>Category</th> <th>Product Price</th> <th>Image</th> <th>Total Price</th> <th class="text-center">Actions</th> </thead> <?php $get = mysqli_query($conn,"SELECT * FROM stock;"); ?> <tbody> <?php while ($row=mysqli_fetch_array($get)) { $id=$row['id']; $company=$row['company_name']; $name=$row['product_name']; $weight=$row['weight']; $price_per_gram=$row['price_per_gram']; $quantity=$row['quantity']; $type=$row['type']; $category=$row['category']; $price=$row['product_price']; $img=$row['img']; $total=$row['total_price']; ?> <tr> <td ><?php echo $id;?></td> <td><?php echo $company;?></td> <td><?php echo $name;?></td> <td><?php echo $weight;?> g</td> <td><?php echo $price_per_gram;?> $</td> <td><?php echo $quantity;?></td> <td><?php echo $type;?></td> <td><?php echo $category;?></td> <td><?php echo $price;?></td> <td> <img src="product_img/<?php echo $img; ?>" style="height:5rem;width:5rem;border-radius:10px;"> </td> <td><?php echo $total;?></td> <td style="width: 20px"> <a href="editstock.php?id=<?php echo $id;?>" class="btn btn-gradient-primary">Edit</a> <button class="btn btn-danger" name="delete" type="submit" value="<?php echo "$id" ?>">Delete</button> </td> </tr> <?php } ?> </tbody> </table> </form> <?php if (isset($_POST['delete'])) { $delete = mysqli_query($conn,"DELETE FROM stock WHERE id= ".$_POST['delete']." "); header("location:viewstcok.php"); } ?>