Jump to content

Problem clearing varriables


c_pattle

Recommended Posts

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

Link to comment
Share on other sites

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");
}
?> 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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");
}
?> 

Link to comment
Share on other sites

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");
}
?> 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.