Page 1 of 1

Password Problem

Posted: Tue Apr 12, 2005 8:28 pm
by Arsenal Rule
I tried this code the and thing is for some reason it it gets stuck at this point

if (!$_POST['password1'] !== $_POST['password2'] || empty($_POST['password1'])) {
echo 'Your passwords do not match Or you did not enter a password. Please enter again';

because I enter the password and made sure its the same password each time but when I click the submit button it keeps printing "'Your passwords do not match Or you did not enter a password. Please enter again" even though the two passwords are the same I made sure all the variables are the same and all the text box names are the same so I dunno what else the problem maybe

Code: Select all

<?php 

if ($_POST['submitForm']) { 
      
    if (!$_POST['password1'] !== $_POST['password2'] || empty($_POST['password1'])) { 
        echo 'Your passwords do not match Or you did not enter a password. Please enter again'; 
    } elseif (!empty($_POST['fname']) && !empty($_POST['sname']) && !empty($_POST['uname'])) { 
        $conn = mysql_connect("localhost","root","") or die("Could Not Connect To The Database"); 
        mysql_select_db("database",$conn) or die("Could Not Select The Database"); 
        $res = mysql_query("SELECT username FROM passwordtable WHERE username='$_POST[uname]'",$conn) or die("Query Failed"); 
        $exists = mysql_num_rows($res); 
                  
        if ($exists !== 0) { 
            echo 'That Username Already Exists In The Database. Please Choose Another User Name Thank You.'; 
        } else { 
            $sql = "INSERT INTO passwordtable (user_id,firstname,surname,username,password) VALUES ('','$_POST[fname]','$_POST[sname]','$_POST[uname]','$_POST[password1]')"; 
            $query = mysql_query($sql,$conn) or die(mysql_error("Could Not Write Information To The Database")); 
              
            if (mysql_affected_rows($conn) == 0) { 
                echo 'Your Account Was Not Created.'; 
            } else { 
                echo 'Your Account Was Created Successfully'; 
            } 
        }      
    } 
} 

?>

        <p align="center"><form action="register.php" method="post" >
		<p><STRONG>First Name:</STRONG><br>
		<input type="text" name="fname" maxlength="20"></p>
		<p><STRONG>Surname:</STRONG><br>
		<input type="text" name="sname" maxlength="20"></p>
		<p><STRONG>User Name:</STRONG><br>
		<input type="text" name="uname" maxlength="20"></p>
		<p><STRONG>Password:</STRONG><br>
		<input type="password" name="password1" maxlength="32"></p>
		<p><STRONG>Confirm Password:</STRONG><br>
		<input type="password" name="password2" maxlength="32"></p>
		<p><input type="submit" name="submitForm" value="Register User"></p>
		</form>
		</p>


This is what I done but it still doesn't seem to work even though I enter the same password it still comes up with "'Your passwords do not match Or you did not enter a password. Please enter again" so I hope anyone can help me to find what the problem is it would be great

Thank you so much :)

Posted: Tue Apr 12, 2005 9:38 pm
by Burrito
get rid of the first bang.

otherwise you're using like a double negative, and didn't your mama not never teach you to not never do that?

Code: Select all

if (($_POST['password1'] !== $_POST['password2']) || empty($_POST['password1'])) {
I didn't look at the rest of your code, but try that first...

Posted: Tue Apr 12, 2005 10:18 pm
by infolock
You have a lot of errors in your code. I've changed them accordingly. Try this code...

Code: Select all

<?php 
    if ($_POST['password1'] != $_POST['password2'] || !isset($_POST['password1']) || !isset($_POST['password2')
	{
        echo 'Your passwords do not match or you did not enter a password. Please enter again';
		exit;
    }
	if(!isset($_POST['fname']) || !isset($_POST['sname']) || !isset($_POST['uname']))
	{
		echo 'You must enter All fields before continuing';
		exit;
	}
        $conn = mysql_connect("localhost","root","") or die("Could Not Connect To The Database"); 
        mysql_select_db("database",$conn) or die("Could Not Select The Database"); 
		//you did not set the $_POST var correctly.
        $res = mysql_query("SELECT username FROM passwordtable WHERE username='".$_POST['uname']."'",$conn) or die("Query Failed"); 
        $exists = mysql_num_rows($res); 
                  
        if ($exists != 0) { 
            echo 'That Username Already Exists In The Database. Please Choose Another User Name Thank You.'; 
        } else { 
//Again, $_POST vars were not set correctly.
            $sql = "INSERT INTO passwordtable (user_id,firstname,surname,username,password) VALUES ('','".$_POST['fname']."','".$_POST['sname']."','".$_POST['uname']."','".$_POST['password1']."')"; 
            $query = mysql_query($sql,$conn) or die(mysql_error("Could Not Write Information To The Database")); 
            if (mysql_affected_rows($conn) == 0) { 
                echo 'Your Account Was Not Created.'; 
            } else { 
                echo 'Your Account Was Created Successfully'; 
            } 
        }      
    } 
} 

?>

        <p align="center"><form action="register.php" method="post" >
		<p><STRONG>First Name:</STRONG><br>
		<input type="text" name="fname" maxlength="20"></p>
		<p><STRONG>Surname:</STRONG><br>
		<input type="text" name="sname" maxlength="20"></p>
		<p><STRONG>User Name:</STRONG><br>
		<input type="text" name="uname" maxlength="20"></p>
		<p><STRONG>Password:</STRONG><br>
		<input type="password" name="password1" maxlength="32"></p>
		<p><STRONG>Confirm Password:</STRONG><br>
		<input type="password" name="password2" maxlength="32"></p>
		<p><input type="submit" name="submitForm" value="Register User"></p>
		</form>
		</p>
also, is the above html data in the same file as the script? ie, does all the above code (the script and the form) go into yourfile.php ? if so, you need to change the action to this :

Code: Select all

<p align="center"><form action="<?php echo $PHP_SELF; ?>" method="post" >
hope this helps...

Password Problem

Posted: Wed Apr 13, 2005 3:10 am
by Arsenal Rule
Hey thanx for your help. I will try your suggestion, Yes the html form is in the same php file as the PHP coding they are both in file called register.php Also I just asking you took off the bit which links the button to the code this bit here

Code: Select all

if ($_POST['submitForm']) {.
because I thought this is what links the button created on the form to the PHP coding. So just asking you if there is a link from the button to the code in your edited code you gave me?

Thanks for your help mate appreciate it Thankyou

Password Problem

Posted: Wed Apr 13, 2005 4:41 am
by Arsenal Rule
Hey mate I made the changes you showed me and it seems to be entering the data fine into the database, but its not carrying out the validation check of leaving the text box empty it because its ment to give a warning if its left empty but it doesn't it still save it to the database so just wondering if you know how to correct this.

Code: Select all

<?php 

	if ($_POST['submitForm']) { 
      
    if ($_POST['password1'] != $_POST['password2'] || !isset($_POST['password1']) || !isset($_POST['password2']))
	{ 
        echo 'Your Passwords Does Not Match Or You Did Not Enter A Password. Please Enter Them Again';
		exit;
	}
	if(!isset($_POST['fname']) || !isset($_POST['sname']) || !isset($_POST['uname']))
	{
		echo 'You Must Enter And Complete All fields Before Continuing';
		exit;
	}
		$conn = mysql_connect("localhost","root","") or die("Could Not Connect To The Database"); 
		mysql_select_db("database",$conn) or die("Could Not Select The Database"); 
		//you did not set the $_POST var correctly.
		$res = mysql_query("SELECT username FROM password WHERE username='".$_POST['uname']."'",$conn) or die("Query Failed");
		$exists = mysql_num_rows($res);
		
		if ($exists != 0) {
			echo 'That Username Already Exists In The Database. Please Choose Another User Name Thank You.';
		} else {
		//Again, $_POST vars were not set correctly.
			$sql = "INSERT INTO password (user_id,az_firstname,az_surname,username,password) VALUES ('','".$_POST['fname']."','".$_POST['sname']."','".$_POST['uname']."','".$_POST['password1']."')";
			$query = mysql_query($sql,$conn) or die(mysql_error("Could Not Write Information To The Database"));
			if (mysql_affected_rows($conn) == 0) {
				echo 'Your Account Was Not Created.';
			} else {
				echo 'Your Account Was Created Successfully';
			} 
        }      
    } 
	 
?>		      

        <p align="center"><form action="<?php echo $PHP_SELF; ?>" method="post" >
		<p><STRONG>First Name:</STRONG><br>
		<input type="text" name="fname" maxlength="20"></p>
		<p><STRONG>Surname:</STRONG><br>
		<input type="text" name="sname" maxlength="20"></p>
		<p><STRONG>User Name:</STRONG><br>
		<input type="text" name="uname" maxlength="20"></p>
		<p><STRONG>Password:</STRONG><br>
		<input type="password" name="password1" maxlength="32"></p>
		<p><STRONG>Confirm Password:</STRONG><br>
		<input type="password" name="password2" maxlength="32"></p>
		<p><input type="submit" name="submitForm" value="Register User"></p>
		</form>
		</p>
Also Can I ask you some thing mate I created a form to view this information. I just wanna know how would be able to view the information from the data above in the form that I have created

I hope you can help me with this that would be great

Thankyou

:D