In the /list/index.php
page, transform the delete button of an article in link (i.e. to another page) while keeping the visual aspect of the button. This link should redirect to the page /list/remove.php?id=_____&key=________________
:
id
is the identifier of the article;key
is the security key for the list.Create the script remove.php
and check that the data is correctly received by the script.
Before deleting the article, we'll check that the security key is well associated with the article. Specifically, we will verify that the key is property associated with the list that is associated with the item.
The problem here is that the security key is not directly in the articles
table.
A solution would be to proceed in two steps:
MySQL is a relational language which allows to perform these two operations in a single request through a mechanism called a join. Here is the code that allows to retrieve the security key directly from the item ID:
SELECT articles.*, lists.ukey FROM articles INNER JOIN lists ON lists.id = articles.id_list WHERE articles.id = 5
Test the function in phpMyAdmin, and make sure you retrieve the data correctly. of the article, as well as the security key:
For the deletion of an item, we could proceed in two steps: the check and then delete. To optimize our code, we will only perform only one request with a join. Based on the previous join, write and test in phpMyAdmin a request for deletion of an article by its identifier and its security key. The end of the query must be of the type :
... WHERE articles.id = :id AND lists.ukey=:key
In the library /lib/lib.php
add a function removeArticle($db, $id_article, $key)
.
which deletes an item passed as a parameter by its identifier and its security key (only if the security key is valid, otherwise the article is not deleted).
Once the article is deleted, redirect the user to the previous page at Use the following instruction:
header('Location: ' . $_SERVER['HTTP_REFERER']);