More than just a guestbook 3
By Albert Tagaban
In the last two articles in Hardcode, I discussed how you can develop a not-so-ordinary guest book and we will still continue working on the guest book until such time that we have done all that we can think of. This time, we are going to work on the administrator panel that will allow the administrator to manage the guest book entries.
When I say manage, it includes deleting and editing guest book entries. We can also add a settings page that will let the administrator change the settings, such as whether to allow HTML codes, and the number of guest book entries to be shown per page. We can also add a feature that will allow the admin to add another administrator to help him manage the guest book entries. Of course, it is important to have a log-in system but it is best if we use HTTP authentication; but then it is even better if we use both HTTP authentication and the log-in system.
Assuming that the log-in system and settings page are done, we now go to the last task that will delete or edit a guest book entry. We don’t have to create this page all over again from scratch and we can re-use the page we have for the guest book in the last two articles. All we have to do is add a link that will allow the administrator to edit or delete an entry. Since we were able to display those guest book entries using a loop, we are going to put those links inside the loop too.
We can link those links in just one page or with two different scripts depending on which option you prefer; but for this one I will be using a single page to do either edit or delete a guest book entry. For me to do that, I will set the value of the href attribute of the anchor tag for the edit link like this
editedelete.php?action=edit&guestbookid=<?=$guestbookid;?>
where $guestbookid is the ID of the guest book entry that’s included in your query and the editdelete.php is the script to be executed. As for the delete page, I will just replace the edit in the above snippet with delete.
We now go to the page that will either delete or edit guest book entries. Whichever action the user wants to do, we will need to get the details of that guest book entry and that includes the message, name, e-mail, and website (if any). So, we are going to assign the HTTP GET variable guestbookid and validate if it is of a numeric type; then we will query the database and have the where clause search for the guest book entry that is equal to the guestbookid.
Now that we have the details of the guest book entry, we will now check for the action that the administrator wants to do and set a variable for the action attribute of the proper form and then assign each field of the query to a variable representing each field. The next thing we should do is set the value of the respective form fields assigned to the database fields.
By now, you should be able to see a form with fields having the value of the guestbook entry. The only thing missing is the part that will process those fields if the administrator decides to submit the form fields. The action attribute of the form tag should point to itself with HTTP GET variables of action and the guestbookid. This is the same as the snippet mentioned above that we use in the links. It is also important if we set the method attribute to POST.
Now, let’s work on the real thing. Right below the code that checks for the HTTP GET action, we will add a code that checks if there are forms submitted via the HTTP POST; we then check again for the action. If the action is delete then let’s query the database and delete that guestbook entry. It is better if we just show a message confirming that the action has been made and then forward the page back to the list of guest book entries.
We will do the same thing if the action is edit. Only this time, we are going to use the update query instead of delete. Comments and suggestions are welcome at albert.tagaban@gmail.com You can also visit my blog at http://www.alberttagaban.com
|