Page 1 of 1

Clearing the $_POST variable

Posted: Fri Aug 01, 2003 11:52 am
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).

Posted: Fri Aug 01, 2003 12:04 pm
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.

Posted: Sat Aug 02, 2003 1:28 pm
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.

Posted: Sat Aug 02, 2003 2:01 pm
by nielsene
Yes php callows subroutines, it calls them functions. Check the manual for the format.