Skip to content
Advertisement

“Delete row” button for MySQL in PHP

I have a table that becomes populated with data from a MySQL database, and each row receives its own delete button. I would like to have the option to delete each row separately with a delete button that deletes the corresponding row in the database. How would I go about doing so? Here’s the part of the code that I have to far, which does not seem to work whatsoever.

JavaScript

Unimportant code omitted.

Table/Form Creation:

JavaScript

Advertisement

Answer

Form action needs to have a control with the name ‘id’, otherwise $_POST will not get any id.

You can do it like <input type="hidden" name="id" value=".$row['id']." />

Also, I suggest you have a look at How can I prevent SQL injection in PHP? as your form, by the code you displayed, looks completely vulnerable.

UPDATE

You also have to fix your $delete query like nl-x mentioned:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement