Clearing the $_POST variable

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
chriso
Forum Commoner
Posts: 31
Joined: Fri Aug 01, 2003 11:52 am

Clearing the $_POST variable

Post by chriso »

I have created a form that sends its key/values via POST to a php script called admin.php. The gist of the script is this:
1. check to see if fname has been sent - if (isset($_POST['fname'])
2. if fname has been sent, then add the information to the faculty table (MYSQL database being used) and display the contents of the faculty table. If fname has not been sent, then display the contents of the faculty table.

The basic structure is:
******************************
if (fname present) {
add contents to db
if (@mysql_query($sql)) {
echo (databased updated)
}else {
echo(error)
}
}

Display contents of faculty table
*****************************

The problem I'm having is that after the DB is updated and displayed, if I then hit the refresh button, the member is added again to the DB because I'm essentially resending $_POST again and going through the script.

How can I prevent this from occuring? Should I have put the 'add to database' script in another php file?

Thanks. Also, I'm very new with php and MYSQL (but I'm moderately good with cgi).
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

One option. before adding the entry, check if it already exists. If there already is a record either update to the new info or silently skip the insert.
chriso
Forum Commoner
Posts: 31
Joined: Fri Aug 01, 2003 11:52 am

Post by chriso »

Does php allow subroutines? Since I want the 'faculty' table to be displayed in any case, if subroutines can be written in php, then all I need to do is call the subroutine when I want to display the table.

If subs are acceptable, what is the format for writing one?

Thanks for the help.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

Yes php callows subroutines, it calls them functions. Check the manual for the format.
Post Reply