You should use a post method form, with a hidden field for the id, since you are performing an action on the server. By using a get method link, should a search engine index the page, all the data will get deleted, because it will follow all the links it finds.
Both the form processing code and the code producing the post method form must enforce delete permission and owner-ship or administrator-ship for the id(s) being operated on.
In general, the code for any page should be laid out like this -
- initialization
- post method form processing
- get method business logic - get/produce the data needed to display the page
- html document
For the posted code -
- Use ‘require’ for things your code must have for it to work and require/require_once is not a function. The () around the filename are unnecessary clutter.
- $_GET is always set, even it if is empty. You should trim, then validate all inputs before using them. If a ‘required’ input is empty or in this case isn’t owned by the currently logged in user or the currently logged in user isn’t an administrator, who can operate on all data, this is an error. You would setup user/validation error messages, in an array, using the form field name as the main array index, letting the user know what was wrong with the data that they submitted. After the end of the validation logic, if there are no errors, use the submitted form data. To display the errors, test for and display the content of the array holding the errors in the html document.
- don’t copy variables to other variables for nothing.
- if you use simple ? place-holders and supply an array of values to the ->execute([…]) call, you can reduce the amount of typing.
- upon successful completion of the post method form processing code, you should execute a redirect to the exact same URL of the current page to cause a get request for that page. This will prevent the browser from resubmitting the form data should that page get reloaded or browsed back to.
- every redirect needs an exit/die statement to stop php code execution.