c_pattle Posted May 10, 2010 Share Posted May 10, 2010 Hey I am writing a page that adds a new user into a mysql database. The code all works fine except after the new user has been added if you refresh to page the variables still store the information it attempts to add the user again. I have attempted to clear the variable but it doesn't seem to work. Does anyone know where I am going wrong? <?php //Adding a new user into the database $register_firstname = $_POST['register_firstname']; $register_lastname = $_POST['register_lastname']; $register_username = $_POST['register_username']; $register_email = $_POST['register_email']; $register_password_1 = $_POST['register_password_1']; $register_password_2 = $_POST['register_password_2']; if( $register_firstname and $register_lastname and $register_username and $register_email and $register_password_1 and $register_password_2) { if( $register_password_1 == $register_password_2) { $sql="insert into users (first_name, last_name, username, email, password) values (\"$register_firstname\", \"$register_lastname\", \"$register_username\", \"$register_email\", \"$register_password_1\")"; $rs = mysql_query( $sql, $link ); if($rs) { echo("congratulations you have successfully registered"); } else { echo ('error' . mysql_error()); } //Clearing variables $register_firstname = ''; $register_lastname = ''; $register_username = ''; $register_email = ''; $register_password_1 = ''; $register_password_2 = ''; $_POST['register_firstname'] = ''; $_POST['register_lastname'] = ''; $_POST['register_username'] = ''; $_POST['register_email'] = ''; $_POST['register_password_1'] = ''; $_POST['register_password_2'] = ''; } else { echo ("The two passwords do not match. Please retype them"); } } else { echo ("You have not completed all the fields. Please fill in the blank fields"); } ?> Thanks for any help Quote Link to comment https://forums.phpfreaks.com/topic/201285-problem-clearing-varriables/ Share on other sites More sharing options...
siric Posted May 10, 2010 Share Posted May 10, 2010 Place the variable clearing lines at the top of the script <?php //Adding a new user into the database //Clearing variables $register_firstname = $register_lastname = $register_username = $register_email = $register_password_1 = $register_password_2 = ''; $register_firstname = $_POST['register_firstname']; $register_lastname = $_POST['register_lastname']; $register_username = $_POST['register_username']; $register_email = $_POST['register_email']; $register_password_1 = $_POST['register_password_1']; $register_password_2 = $_POST['register_password_2']; if( $register_firstname and $register_lastname and $register_username and $register_email and $register_password_1 and $register_password_2) { if( $register_password_1 == $register_password_2) { $sql="insert into users (first_name, last_name, username, email, password) values (\"$register_firstname\", \"$register_lastname\", \"$register_username\", \"$register_email\", \"$register_password_1\")"; $rs = mysql_query( $sql, $link ); if($rs) { echo("congratulations you have successfully registered"); } else { echo ('error' . mysql_error()); } //Clearing variables $register_firstname = ''; $register_lastname = ''; $register_username = ''; $register_email = ''; $register_password_1 = ''; $register_password_2 = ''; $_POST['register_firstname'] = ''; $_POST['register_lastname'] = ''; $_POST['register_username'] = ''; $_POST['register_email'] = ''; $_POST['register_password_1'] = ''; $_POST['register_password_2'] = ''; } else { echo ("The two passwords do not match. Please retype them"); } } else { echo ("You have not completed all the fields. Please fill in the blank fields"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/201285-problem-clearing-varriables/#findComment-1056035 Share on other sites More sharing options...
kenrbnsn Posted May 10, 2010 Share Posted May 10, 2010 That won't work. The $_POST array is regenerated when the page is re-submitted. There are two ways of getting around this. One is with a META tag (I have to go back in my notes to find it). The other is to do all of your processing and use the header function to redirect to the same script to redisplay the form. ken Quote Link to comment https://forums.phpfreaks.com/topic/201285-problem-clearing-varriables/#findComment-1056046 Share on other sites More sharing options...
c_pattle Posted May 11, 2010 Author Share Posted May 11, 2010 Thanks Ken. So all I'd need to do is just add in "header('Location: page_script_is_on.php');" after all of the processing has been done like I have below? <?php //Adding a new user into the database //Clearing variables $register_firstname = $register_lastname = $register_username = $register_email = $register_password_1 = $register_password_2 = ''; $register_firstname = $_POST['register_firstname']; $register_lastname = $_POST['register_lastname']; $register_username = $_POST['register_username']; $register_email = $_POST['register_email']; $register_password_1 = $_POST['register_password_1']; $register_password_2 = $_POST['register_password_2']; if( $register_firstname and $register_lastname and $register_username and $register_email and $register_password_1 and $register_password_2) { if( $register_password_1 == $register_password_2) { $sql="insert into users (first_name, last_name, username, email, password) values (\"$register_firstname\", \"$register_lastname\", \"$register_username\", \"$register_email\", \"$register_password_1\")"; $rs = mysql_query( $sql, $link ); if($rs) { echo("congratulations you have successfully registered"); } else { echo ('error' . mysql_error()); } header('Location: page_script_is_on.php'); } else { echo ("The two passwords do not match. Please retype them"); } } else { echo ("You have not completed all the fields. Please fill in the blank fields"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/201285-problem-clearing-varriables/#findComment-1056273 Share on other sites More sharing options...
kenrbnsn Posted May 11, 2010 Share Posted May 11, 2010 Yes, that should work. Ken Quote Link to comment https://forums.phpfreaks.com/topic/201285-problem-clearing-varriables/#findComment-1056529 Share on other sites More sharing options...
c_pattle Posted May 12, 2010 Author Share Posted May 12, 2010 I still can't seem to use clear the variable in this script. Have I gone wrong anywhere or does anyone else know what I can do to try to clear them? <?php //Adding a new user into the database //Clearing variables $register_firstname = $register_lastname = $register_username = $register_email = $register_password_1 = $register_password_2 = ''; $register_firstname = $_POST['register_firstname']; $register_lastname = $_POST['register_lastname']; $register_username = $_POST['register_username']; $register_email = $_POST['register_email']; $register_password_1 = $_POST['register_password_1']; $register_password_2 = $_POST['register_password_2']; if( $register_firstname and $register_lastname and $register_username and $register_email and $register_password_1 and $register_password_2) { if( $register_password_1 == $register_password_2) { $sql="insert into users (first_name, last_name, username, email, password) values (\"$register_firstname\", \"$register_lastname\", \"$register_username\", \"$register_email\", \"$register_password_1\")"; $rs = mysql_query( $sql, $link ); if($rs) { echo("congratulations you have successfully registered"); } else { echo ('error' . mysql_error()); } header('Location: page_script_is_on.php'); } else { echo ("The two passwords do not match. Please retype them"); } } else { echo ("You have not completed all the fields. Please fill in the blank fields"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/201285-problem-clearing-varriables/#findComment-1057247 Share on other sites More sharing options...
cyberRobot Posted May 12, 2010 Share Posted May 12, 2010 I have the same problem with one of my scripts. I haven't gotten around to coding any kind of solution, but here is what I plan to do. Run a quick test against the database for exact duplicates (first name, last name, username, etc.) If a record is found, do nothing Else, add the new record Quote Link to comment https://forums.phpfreaks.com/topic/201285-problem-clearing-varriables/#findComment-1057253 Share on other sites More sharing options...
kenrbnsn Posted May 12, 2010 Share Posted May 12, 2010 I'm not sure what you mean by "clearing the variables". I set up a quick test form at http://rbnsn.com/phpfreaks/simple_form.php There are two fields -- both are required. If you enter something in both fields you will get a message and refreshing the screen won't give you the message from the browser, since I used the header() call to redisplay the form. Ken Quote Link to comment https://forums.phpfreaks.com/topic/201285-problem-clearing-varriables/#findComment-1057270 Share on other sites More sharing options...
c_pattle Posted May 12, 2010 Author Share Posted May 12, 2010 Thanks When I tried to use the header function shown the in script above I got this warning message. Warning: Cannot modify header information - headers already sent by (output started at /var/www/ribbons/register.php:24) in /var/www/ribbons/register.php on line 135 Quote Link to comment https://forums.phpfreaks.com/topic/201285-problem-clearing-varriables/#findComment-1057275 Share on other sites More sharing options...
cyberRobot Posted May 12, 2010 Share Posted May 12, 2010 The header function needs to be called before anything is printed to the screen. Quote Link to comment https://forums.phpfreaks.com/topic/201285-problem-clearing-varriables/#findComment-1057277 Share on other sites More sharing options...
kenrbnsn Posted May 12, 2010 Share Posted May 12, 2010 That error means that you've already sent something to the screen before you used the header() function. That's a no-no. Do all your processing at the start of your script before anything is sent to the browser. Ken Quote Link to comment https://forums.phpfreaks.com/topic/201285-problem-clearing-varriables/#findComment-1057278 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.