1

So I'm creating a database for my website and I want to create an admin section that allows you to add or delete from the table. Here is a snapshot of what I want to achieve...

enter image description here

In my php file I have if($_POST['delete_category']) which correctly gets the delete button clicks, but then I'm not sure how to distinguish which delete button was actually clicked. I'm sure this is a very simple solution, but I'm stuck. Thanks!

1
  • It's easier if you make them links with the id appended to the URL in the querystring Commented Sep 23, 2014 at 2:00

1 Answer 1

2

You can discern which button is submitted by this following markup (based on your example fetched results from DB):

<?php if(isset($_POST['delete_category'])) { $id = $_POST['delete_category']; // the value="1" or value="3" goes in here echo $id; } ?> <form method="POST" action=""> <table border="1"> <tr> <th>ID</th><th>Name</th><th>Delete</th> </tr> <tr> <td>1</td> <td>Senior Pictures</td> <td><button typpe="submit" name="delete_category" value="1">Delete</button></td> <!-- each delete button has the same name, but different values --> </tr> <tr> <td>3</td> <td>Engagements</td> <td><button typpe="submit" name="delete_category" value="3">Delete</button></td> </tr> </table> </form> 

If I had to guess this makes sense on the fetching: (Note: Just a sample!)

<form method="POST" action=""> <table border="1"> <tr> <th>ID</th><th>Name</th><th>Delete</th> </tr> <?php while($row = $results->fetch_assoc()): ?> <!-- assuming mysqli() --> <?php while($row = mysql_fetch_assoc($result)): ?> <!-- assuming on mysql (bleh) --> <tr> <td><?php echo $row['id']; ?></td> <td><?php echo $row['name']; ?></td> <td> <button typpe="submit" name="delete_category" value="<?php echo $row['id']; ?>">Delete</button> </td> </tr> <?php endwhile; ?> </table> </form> 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.