Jump to content

Recommended Posts

I have this code im writing and i want to make it so if this query

        $result = mysql_query("SELECT * FROM `accounts` where login='$user' AND password='$pass'")

Comes back false it comes back with the error "Invalid username or password" if it comes back true it goes on to do a mysql update which would be

"UPDATE accounts SET password = '$newpass' WHERE username = '$user' and password = '$pass";

and if that doesnt work it just gives the mysql error

If someone could help me do this that would be great

 

Thanks

Blink359

Link to comment
https://forums.phpfreaks.com/topic/156807-quick-mysql-query-problem/
Share on other sites

So this should work

$result = mysql_query("SELECT * FROM `accounts` where login='$user' AND password='$pass'") or die(mysql_error());

        {
        if (empty($result)) return 'Invalid username or password';
        else {
                 $sql = "UPDATE accounts SET password = '$newpass' WHERE username = '$user' and password = '$pass";";
                 $result = mysql_query($sql);
        if mysql_num_rows($result)==0 return mysql_error();
        }

$result = mysql_query("SELECT * FROM `accounts` where login='$user' AND password='$pass'") or die(mysql_error());

       {
       if (empty($result)) return 'Invalid username or password';
       else {
                $sql = "UPDATE accounts SET password = '$newpass' WHERE username = '$user' and password = '$pass";";
                $result = mysql_query($sql);
       if mysql_num_rows($result)==0 return mysql_error();
       }

Syntax error much!?

What needs editing then im newbie remember im trying to learn

also its this now

        $result = mysql_query("SELECT * FROM `accounts` where login='$user' AND password='$pass'") or die(mysql_error());

        {
        if (empty($result)) return 'Invalid username or password';
        else {
                 $sql = "UPDATE accounts SET password = '$newpass' WHERE username = '$user' and password = '$pass";";
                 $result = mysql_query($sql);
        if (empty($result)) return mysql_error();
        }

Parse error: syntax error, unexpected T_STRING in /var/www/nathan/wotlk/passchange.php on line 52

which is aparently my actual form

<form name=myform method=post action=''>
          <input name="user" type="text"/>
          <br>  
          Password: 
          <input name="pass" type="password"/>
          <br>  
         New Password: 
          <input name="newpass" type="password"/>
          <br>  
          Repeat New Password: 
          <input name="newpass2" tpye="password"/>
          <br />
          <input name="Submit" type="submit" value="submit" /> 
          
          <imput name="reset" type="reset" value="reset" /> 
</form>

[*]Don't SELECT * when you are not even retrieving the results, just select one column such as the ID or username.

[*]Don't leave that or die(mysql_error()) code in when this system is live on the internet, it will only invite people to try and hack your script/website.

[*]I hope $user and $pass are being escaped to prevent SQL Injection.

[*]You only intend to update 1 row in your UPDATE, so add LIMIT 1 to the end, why? If someone does have a successful SQL Injection attack there is a possibility of changing all your users passwords in one go. Addling LIMIT 1 will ensure only 1 row is updated.

[*]I can't see the rest of your code but it appears your passwords are being stored as plain text, you should consider encrypting or encoding them

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.