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 th
Most likely because you setup the id="delete". Usually id attribute values are not duplicated.
id="delete"
echo ""; echo "Delete"; The submit button gets the first ID and thus getting the first hidden input. Alternatively, you could devise your button like this and serve as your marker: No need to print each form!. Just wrap it with the table: echo ""; echo ''; while($row = mysqli_fetch_assoc($result)) { $prod_id = $row['prod_id']; echo ""; echo "".$count.""; echo "".$row['prod_id'].""; echo "".$row['prod_name'].""; echo "".$row['prod_price'].""; echo ""; // each id is assigned to each button, so that when its submitted you get the designated id, the one that you clicked echo "Delete"; echo ""; echo ''; } echo ''; echo ''; Then in PHP processing: if(isset($_GET['delete'])) // as usual { include "connection.php"; $prod_id = $_GET['delete']; // get the id // USE PREPARED STATEMENTS!!! $del="DELETE FROM products WHERE prod_id = ?"; $delete = $link->prepare($del); $delete->bind_param('i', $prod_id); $delete->execute(); // don't echo anything else, because you're going to use header if($delete->affected_rows > 0) { header('location:show_db.php'); } else { echo 'Sorry delete did not push thru!'; } } 0 讨论(0) 查看其它3个回答 发布评论: 提交评论 加载中... 验证码 看不清? 提交回复 热议问题
The submit button gets the first ID and thus getting the first hidden input.
Alternatively, you could devise your button like this and serve as your marker:
No need to print each form!. Just wrap it with the table:
echo ""; echo ''; while($row = mysqli_fetch_assoc($result)) { $prod_id = $row['prod_id']; echo ""; echo "".$count.""; echo "".$row['prod_id'].""; echo "".$row['prod_name'].""; echo "".$row['prod_price'].""; echo ""; // each id is assigned to each button, so that when its submitted you get the designated id, the one that you clicked echo "Delete"; echo ""; echo ''; } echo ''; echo '';
Then in PHP processing:
if(isset($_GET['delete'])) // as usual { include "connection.php"; $prod_id = $_GET['delete']; // get the id // USE PREPARED STATEMENTS!!! $del="DELETE FROM products WHERE prod_id = ?"; $delete = $link->prepare($del); $delete->bind_param('i', $prod_id); $delete->execute(); // don't echo anything else, because you're going to use header if($delete->affected_rows > 0) { header('location:show_db.php'); } else { echo 'Sorry delete did not push thru!'; } }