Jump to content

[SOLVED] simple validation code not working as planned


fri3ndly

Recommended Posts

Hello all, im fairly new to php and a mysql beginner.

 

I have created a login system for users that will contain their own downloads page, however I am trying to implement a change password screen.

 

So far I have come up with the following, but I always get 'Error, query failed' and the password is not updated:

 

FORM:

<div id="password">
<form name="password" method="post" action="<?php echo $PHP_SELF; ?>">
  <p>Current Password:
    <label>
<br/>
    <input name="currentpass" type="password" id="currentpass" size="10" maxlength="15">
    </label>
  </p>
  <p>New Password:
    <label> <br/>
    <input name="newpass" type="password" id="newpass" size="10" maxlength="15">
    </label>
</p>
  <p>Confirm New Password:
    <label> <br/>
    <input name="confirmnewpass" type="password" id="confirmnewpass" size="10" maxlength="15">
    </label>
</p>
  <p>
    <label>
    <input type="submit" name="submit" value="Submit">
    </label>
  </p>
</form>
</div>

 

SCRIPT:

 

<?php
	// retrieve the session information
	$u = $_SESSION['username'];
	$uid = $_SESSION['loginid'];

	// Post variables  
	$currentpass = $_POST['currentpass'];
	$newpass = $_POST['newpass'];
	$confirmnewpass = $_POST['confirmnewpass'];
	$submit = $_POST['submit'];

	//This code runs if the form has been submitted 
	if (isset($submit)) { 

	//This makes sure they did not leave any fields blank  
	if (!$currentpass | !$newpass | !$confirmnewpass) {
	echo'<p>You did not complete all of the required fields.</p>';
	error==1;
	}

	// checks database password     

	$passcheck = mysql_query("SELECT password FROM login WHERE username='$u'") or die(mysql_error());

	$row = mysql_fetch_assoc( $passcheck );
	$passcheck = $row['password'];

    // checks entered password  
	$currentpass = md5(strip_tags($currentpass));

	// compares passwords  
	if ($passcheck != $currentpass) {
	echo '<p>Incorrect password.</p>';
    $error==1;
	}

	// this makes sure both passwords entered match  
	if ($confirmnewpass != $newpass) {
	echo'<p>Your new passwords did not match.</p>';
	$error==1;
	}

	// here we encrypt the password and add slashes if needed 
	$newpass = md5(strip_tags($newpass));
	if (!get_magic_quotes_gpc()) {
	$newpass = addslashes($newpass);
	}

	// now we insert it into the database
	//$query = "UPDATE login SET password = PASSWORD('$newpass')". "WHERE user = '$u'";

	$query = "UPDATE login SET password = '$newpass' WHERE user = '$u'";

	echo "$newpass";

	mysql_query($query) or die('Error, query failed');



	$error==0;

	// now we let them know if their password change was succesful
	if ($error==0){
	echo "<p>Your Password has been changed.</p>";
	}

	}else{

	echo 'There was an error whilst changing your password';
	}
?>

 

It is not working and I cannot figure out why. The script also produces multiple messages which I need to sort out.

 

Can somebody lead me in the right direction as to making sure each section is complete before mysql looks at the next, and tell me what is wrong?

 

Thanks

 

 

Link to comment
Share on other sites

Well, what's the content of $_SESSION['username']?

 

<?php
	// retrieve the session information
                echo $_SESSION['username'];
?>

 

Post the errors, too. Not enough info here. I'm assuming at this point.

Link to comment
Share on other sites

Well, what's the content of $_SESSION['username']?

 

<?php
	// retrieve the session information
                echo $_SESSION['username'];
?>

 

This simply checks to see the session has started and check their username, in this case 'admin'

 

Post the errors, too. Not enough info here. I'm assuming at this point.

 

The error is 'Unknown column 'user' in 'where clause' - lol ignore me I am such an idiot, i was checking for 'user' instead of 'username'

 

Thanks for telling me how to output the error

Link to comment
Share on other sites

The script now works, but my problem is validation.

 

Please can someone show me how I can make this script only show 'Your password has been updated' if it has been updated. At the moment you will see at the bottom of the script it compares the entered password to the database password, but this method only works if you refresh the page as the script has already run, adn from then on it shows it on every page....

 

<?php
	// retrieve the session information
	$u = $_SESSION['username'];
	$uid = $_SESSION['loginid'];

	// Post variables
	$currentpass = $_POST['currentpass'];
	$newpass = $_POST['newpass'];
	$confirmnewpass = $_POST['confirmnewpass'];
	$submit = $_POST['submit'];

	//This code runs if the form has been submitted 
	if (isset($submit)) { 
	$errors=0;

	//This makes sure they did not leave any fields blank  
	if (!$currentpass | !$newpass | !$confirmnewpass) {
	$errors=1;
	echo'<p>You did not complete all of the required fields.</p>';
	}

	// checks database password     
	$passcheck = mysql_query("SELECT password FROM login WHERE username='$u'") or die(mysql_error());
	$row = mysql_fetch_assoc( $passcheck );
	$passcheck = $row['password'];

    // checks entered password  
	$currentpass = md5(strip_tags($currentpass));

	// compares passwords  
	if ($passcheck != $currentpass) {
	$errors=1;
	echo '<p>- Incorrect password.</p>';
	}

	// this makes sure both passwords entered match  
	if ($confirmnewpass != $newpass) {
	$errors=1;
	echo'<p>- Your new passwords did not match.</p>';
	}


	// here we encrypt the password and add slashes if needed 
	$newpass = md5(strip_tags($newpass));
	if (!get_magic_quotes_gpc()) {
	$newpass = addslashes($newpass);
	}

	// now we insert it into the database		
	$query = "UPDATE login SET password = '$newpass' WHERE username = '$u'";
	mysql_query($query) or die(mysql_error());

	// re-check database password to see if the update has taken place

	$passcheck = mysql_query("SELECT password FROM login WHERE username='$u'") or die(mysql_error());
	$row = mysql_fetch_assoc( $passcheck );
	$passcheck = $row['password'];

	// now we let them know if their password change was succesful
	if ($passcheck==$newpass){
		echo "<p>Your Password has been changed.</p>";

	}else{
		echo "There was an error whilst changing your password";
		}
	}

?>

Link to comment
Share on other sites

You need to name your queries differently. Try this

 

<?php
	// retrieve the session information
	$u = $_SESSION['username'];
	$uid = $_SESSION['loginid'];

	// Post variables
	$currentpass = $_POST['currentpass'];
	$newpass = $_POST['newpass'];
	$confirmnewpass = $_POST['confirmnewpass'];
	$submit = $_POST['submit'];

	//This code runs if the form has been submitted 
	if (isset($submit)) { 
	$errors=0;

	//This makes sure they did not leave any fields blank  
	if (!$currentpass | !$newpass | !$confirmnewpass) {
	$errors=1;
	echo'<p>You did not complete all of the required fields.</p>';
	}

	// checks database password     
	$passcheck = mysql_query("SELECT password FROM login WHERE username='$u'") or die(mysql_error());
	$row = mysql_fetch_assoc( $passcheck );
	$passcheck = $row['password'];

    // checks entered password  
	$currentpass = md5(strip_tags($currentpass));

	// compares passwords  
	if ($passcheck != $currentpass) {
	$errors=1;
	echo '<p>- Incorrect password.</p>';
	}

	// this makes sure both passwords entered match  
	if ($confirmnewpass != $newpass) {
	$errors=1;
	echo'<p>- Your new passwords did not match.</p>';
	}


	// here we encrypt the password and add slashes if needed 
	$newpass = md5(strip_tags($newpass));
	if (!get_magic_quotes_gpc()) {
	$newpass = addslashes($newpass);
	}

	// now we insert it into the database		
	$query = "UPDATE login SET password = '$newpass' WHERE username = '$u'";
	mysql_query($query) or die(mysql_error());



	// re-check database password to see if the update has taken place

	$passcheck1 = mysql_query("SELECT password FROM login WHERE username='$u'") or die(mysql_error());
	$row = mysql_fetch_assoc( $passcheck );
	$passcheck1 = $row['password'];

	// now we let them know if their password change was succesful
	if ($passcheck1==$newpass){
		echo "<p>Your Password has been changed.</p>";

	}else{
		echo "There was an error whilst changing your password";
		}
	}

?>

Link to comment
Share on other sites

this should work

 

<?php
	// retrieve the session information
	$u = $_SESSION['username'];
	$uid = $_SESSION['loginid'];

	// Post variables
	$currentpass = $_POST['currentpass'];
	$newpass = $_POST['newpass'];
	$confirmnewpass = $_POST['confirmnewpass'];
	$submit = $_POST['submit'];

	//This code runs if the form has been submitted 
	if (isset($submit)) { 
	$errors=0;

	//This makes sure they did not leave any fields blank  
	if (!$currentpass | !$newpass | !$confirmnewpass) {
	$errors=1;
	echo'<p>You did not complete all of the required fields.</p>';
	}

	// checks database password     
	$passcheck = mysql_query("SELECT password FROM login WHERE username='$u'") or die(mysql_error());
	$row = mysql_fetch_assoc( $passcheck );
	$passcheck = $row['password'];

    // checks entered password  
	$currentpass = md5(strip_tags($currentpass));

	// compares passwords  
	if ($passcheck == $currentpass) {
	$errors=1;
	echo '<p>- Incorrect password.</p>';
	}

	// this makes sure both passwords entered match  
	if ($confirmnewpass != $newpass) {
	$errors=1;
	echo'<p>- Your new passwords did not match.</p>';
	}


	// here we encrypt the password and add slashes if needed 
	$newpass = md5(strip_tags($newpass));
	if (!get_magic_quotes_gpc()) {
	$newpass = addslashes($newpass);
	}

	// now we insert it into the database		
	$query = "UPDATE login SET password = '$newpass' WHERE username = '$u'";
	mysql_query($query) or die(mysql_error());



	// re-check database password to see if the update has taken place

	$passcheck1 = mysql_query("SELECT password FROM login WHERE username='$u'") or die(mysql_error());
	$row = mysql_fetch_assoc( $passcheck1 );
	$passcheck1 = $row['password'];

	// now we let them know if their password change was succesful
	if ($passcheck1==$newpass){
		echo "<p>Your Password has been changed.</p>";

	}else{
		echo "There was an error whilst changing your password";
		}
	}

?>

Link to comment
Share on other sites

The errors=0 doesnt actually mean anything as it is not requested in the result, forgot I left it in there.

 

-------------------------------------------------------------------------------------------

 

Thanks, now it says '- Incorrect password' and 'Your password has been updated'. It also updates the password.

 

 

 

Sorry to be a pain!!

Link to comment
Share on other sites

was my mistake. This should do the trick

 

<?php
	// retrieve the session information
	$u = $_SESSION['username'];
	$uid = $_SESSION['loginid'];

	// Post variables
	$currentpass = $_POST['currentpass'];
	$newpass = $_POST['newpass'];
	$confirmnewpass = $_POST['confirmnewpass'];
	$submit = $_POST['submit'];

	//This code runs if the form has been submitted 
	if (isset($submit)) { 
	$errors=0;

	 // checks entered password  
	$currentpass = md5(strip_tags($currentpass));

	// compares passwords  
	if ($passcheck == $currentpass) {
	$errors=1;
	echo '<p>- Incorrect password.</p>';
	}
	else
{
	//This makes sure they did not leave any fields blank  
	if (!$currentpass | !$newpass | !$confirmnewpass) {
	$errors=1;
	echo'<p>You did not complete all of the required fields.</p>';
	}

	// checks database password     
	$passcheck = mysql_query("SELECT password FROM login WHERE username='$u'") or die(mysql_error());
	$row = mysql_fetch_assoc( $passcheck );
	$passcheck = $row['password'];

	// this makes sure both passwords entered match  
	if ($confirmnewpass != $newpass) {
	$errors=1;
	echo'<p>- Your new passwords did not match.</p>';
	}


	// here we encrypt the password and add slashes if needed 
	$newpass = md5(strip_tags($newpass));
	if (!get_magic_quotes_gpc()) {
	$newpass = addslashes($newpass);
	}

	// now we insert it into the database		
	$query = "UPDATE login SET password = '$newpass' WHERE username = '$u'";
	mysql_query($query) or die(mysql_error());



	// re-check database password to see if the update has taken place

	$passcheck1 = mysql_query("SELECT password FROM login WHERE username='$u'") or die(mysql_error());
	$row = mysql_fetch_assoc( $passcheck1 );
	$passcheck1 = $row['password'];

	// now we let them know if their password change was succesful
	if ($passcheck1==$newpass){
		echo "<p>Your Password has been changed.</p>";

	}else{
		echo "There was an error whilst changing your password";
		}
	}
	}

?>

Link to comment
Share on other sites

<?php
	// retrieve the session information
	$u = $_SESSION['username'];
	$uid = $_SESSION['loginid'];

	// Post variables
	$currentpass = $_POST['currentpass'];
	$newpass = $_POST['newpass'];
	$confirmnewpass = $_POST['confirmnewpass'];
	$submit = $_POST['submit'];

	//This code runs if the form has been submitted 
	if (isset($submit)) { 
	$errors=0;

	 // checks entered password  
	$currentpass = md5(strip_tags($currentpass));

	// compares passwords  
	if ($passcheck == $currentpass) {
	$errors=1;
	echo '<p>- Incorrect password.</p>';
	}
	else
{
	//This makes sure they did not leave any fields blank  
	if (!$currentpass | !$newpass | !$confirmnewpass) {
	$errors=1;
	echo'<p>You did not complete all of the required fields.</p>';
	}

	// checks database password     
	$passcheck = mysql_query("SELECT password FROM login WHERE username='$u'") or die(mysql_error());
	$row = mysql_fetch_assoc( $passcheck );
	$passcheck = $row['password'];

	// this makes sure both passwords entered match  
	if ($confirmnewpass == $newpass) {
	$errors=1;
	echo'<p>- Your new passwords did not match.</p>';



	// here we encrypt the password and add slashes if needed 
	$newpass = md5(strip_tags($newpass));
	if (!get_magic_quotes_gpc()) {
	$newpass = addslashes($newpass);
	}

	// now we insert it into the database		
	$query = "UPDATE login SET password = '$newpass' WHERE username = '$u'";
	mysql_query($query) or die(mysql_error());



	// re-check database password to see if the update has taken place

	$passcheck1 = mysql_query("SELECT password FROM login WHERE username='$u'") or die(mysql_error());
	$row = mysql_fetch_assoc( $passcheck1 );
	$passcheck1 = $row['password'];

	// now we let them know if their password change was succesful
	if ($passcheck1==$newpass){
		echo "<p>Your Password has been changed.</p>";

	}
	}
	else{
		echo "There was an error whilst changing your password";
		}
	}
	}

?>

 

Hopefully that will work. I am at uni and can't test the code

Link to comment
Share on other sites

<?php
	// retrieve the session information
	$u = $_SESSION['username'];
	$uid = $_SESSION['loginid'];

	// Post variables
	$currentpass = $_POST['currentpass'];
	$newpass = $_POST['newpass'];
	$confirmnewpass = $_POST['confirmnewpass'];
	$submit = $_POST['submit'];

	//This code runs if the form has been submitted 
	if (isset($submit)) { 
	$errors=0;

	 // checks entered password  
	$currentpass = md5(strip_tags($currentpass));

	// compares passwords  
	if ($passcheck == $currentpass) {
	$errors=1;
	echo '<p>- Incorrect password.</p>';
	}
	else
{
	//This makes sure they did not leave any fields blank  
	if (!$currentpass | !$newpass | !$confirmnewpass) {
	$errors=1;
	echo'<p>You did not complete all of the required fields.</p>';
	}

	// checks database password     
	$passcheck = mysql_query("SELECT password FROM login WHERE username='$u'") or die(mysql_error());
	$row = mysql_fetch_assoc( $passcheck );
	$passcheck = $row['password'];

	// this makes sure both passwords entered match  
	if ($confirmnewpass == $newpass) {

	// here we encrypt the password and add slashes if needed 
	$newpass = md5(strip_tags($newpass));
	if (!get_magic_quotes_gpc()) {
	$newpass = addslashes($newpass);
	}

	// now we insert it into the database		
	$query = "UPDATE login SET password = '$newpass' WHERE username = '$u'";
	mysql_query($query) or die(mysql_error());



	// re-check database password to see if the update has taken place

	$passcheck1 = mysql_query("SELECT password FROM login WHERE username='$u'") or die(mysql_error());
	$row = mysql_fetch_assoc( $passcheck1 );
	$passcheck1 = $row['password'];

	// now we let them know if their password change was succesful
	if ($passcheck1==$newpass){
		echo "<p>Your Password has been changed.</p>";

	}
	}
	else{
		echo "There was an error whilst changing your password";
		$errors=1;
		}
	}
	}

?>

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.