Jump to content

[SOLVED] Variable Validation not working with IF-ELSE statement


ifis

Recommended Posts

I am trying to create a form to update a password.  I want people to enter the password twice, so it can be compared as the same before being updated, or if not the same, have the person re-enter the password.

<form id='form1' name='form1' method='post' action='updatepassword.php' onsubmit='return validateForm(this)'>
            <table width='500' border='0' cellspacing='0' cellpadding='0'>
              <tr>
                <td>Enter new Password:</td>
                <td><label>
                  <input name='Password1' type='password' id='password1' maxlength='20' />
                </label></td>
              </tr>
              <tr>
                <td>Re-enter Password:</td>
                <td><input name='Password2' type='password' id='password2' maxlength='20' /></td>
              </tr>
              <tr>
                <td></td>
                <td><label>
                <input type='submit' name='Submit' id='Submit' value='Submit' />
                </label></td>
              </tr>
            </table>";

php

<?PHP 
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

//clean data
function clean_data($string)
{
if (get_magic_quotes_gpc())
	{
	$string=stripslashes($string);
	}
return mysql_real_escape_string($string);
}

//password from form
$password1=clean_data($_POST['password1']);
$password2=clean_data($_POST['password2']);

//check isfpasssword 1 and 2 are the same
if (password1 == password2){
//update database
mysql_query("UPDATE Member SET password='$password1' WHERE loginName='{$_SESSION['myusername']}'");

echo "Your password has been updated.  It will be necessary to use it next time you login.";
}
else {
echo "Both passwords do not match.  Please re-enter password";
?>

Thanks

Link to comment
Share on other sites

I'm guessing that it probably has to do with the fact that on your original form you have a capital letter in the variable name..

 

<input name='Password1' type='password' id='password1' maxlength='20' />

 

Change the variable names to all lower case "password1" vs. "Password1"

 

Also...

While not as important you should Null set your variables to prevent someone from "doing something bad"...

 

$password1 = isSet($_POST['password1']) ? clean_data($_POST['password1']) : NULL;

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.