I am pulling a list of products from my MYSQL database and using a delete button against each product in case the operator wants to delete the product.
The problem is that every time I hit the delete button on any product in the list, the first element gets deleted.
What's wrong with my code below ?
Products page:
<?php
$link=mysqli_connect("localhost","root","","smartcart");
$prod="select * from products";
$rw=mysqli_query($link,$prod) or die(mysqli_errno()."in query $prod");
$count=1;
while($row=mysqli_fetch_assoc($rw))
{
echo "<tr>";
echo "<td>".$count."</td>";
echo "<td>".$row['prod_id']."</td>";
echo "<td>".$row['prod_name']."</td>";
echo "<td>".$row['prod_price']."</td>";
echo "<td><form action='delete_prod.php' id='delete' method='get'>";
echo "<input type='hidden' name='prod_id' value='".$row['prod_id']."' />";
echo "<button type='submit' form = 'delete' class='btn btn-default' name='delete'>Delete</button>";
echo "</form></td>";
$count=$count+1;
}
mysqli_free_result($rw);
?>
delete_prod.php:
<?php
if(isset($_GET['delete']))
{
include "connection.php";
$prod_id=$_REQUEST['prod_id'];
$del="delete from products where prod_id=$prod_id";
if (mysqli_query($link,$del))
{
echo "Successfully deleted";
unset($_POST['delete']);
}
else
{
echo "Delete operation Failed";
}
header('location:show_db.php');
}
?>
I think I am terribly missing some simple point, but am unable to get what is it.
$_GET,$_POSTor$_REQUESTdirectly! Clean it first, even if you're sure it's ok!$_REQUEST['prod_id'], but it is the ID of the first product, not of the one which I want to delete. No, no error.prod_idof the product. When I hit the delete button, this particularprod_idis supposed to be carried to thedelete_prod.phppage. But instead, theprod_idof the first product listed is carried.