开发者

MYSQL Update Value Recognition

开发者 https://www.devze.com 2023-02-07 07:25 出处:网络
I am doing an update script for a form.When user information is submitted on sign up its submitted to the user db with the following fields, username, pass, full_name and telephone.When the user updat

I am doing an update script for a form. When user information is submitted on sign up its submitted to the user db with the following fields, username, pass, full_name and telephone. When the user updates the form to change his/her name for example, the username, pass, tele fields are also a part of the update form. So when a user updates his or her telephone # on the update form, the username info and pass info and the name info are also present on the form. The issue is that the system recognizes that the username is already taken.

So how do i update my script in such a way that the values changed should be picked up by the system. Or is there a better way to do this?

Code:

          if (!get_magic_quotes_gpc()) {
           $_POST['username'] = addslashes($_POST['username']);
       }
        $idcheck = $_POST['username'];
        $check = mysql_query("SELECT username FROM users WHERE username = '$username'")
          or die(mysql_error());
        $check2 = mysql_num_rows($check);


         if ($check2 != 0) {
         print("username already taken");
开发者_StackOverflow         die('');
           }

Basically the issue is that it recognizes that the values already exist but it doesn't realize that its the same users info not some other user using a common id or username...


I think the code you posted before was right, if you mean to check if the username is already taken in case a user decides to change his own username, just run this piece of code you posted now before the UPDATE statement, which would be running just in case the username is fine (or allowing it to change the other fileds while leaving the old username and echoing a warning of already taken username)


Just remove the following code from the update script:

$idcheck = $_POST['username'];
$check = mysql_query("SELECT username FROM users WHERE username = '$username'") or die(mysql_error());
$check2 = mysql_num_rows($check);

if ($check2 != 0) {
   print("username already taken");
   die('');
}

And things should be fine then.

If you have the same script for signing up and updating, I suggest you make two different scripts as that is a better practice.

Best, Kamran

0

精彩评论

暂无评论...
验证码 换一张
取 消