Page 1 of 1

register

Posted: Sun Aug 10, 2003 2:47 am
by sguy
what problem with tihs code?
this is a register code, i have 2 tables, account & account1, if i register a user, i don't want the 2 tables have the same username

Code: Select all

$mysql_access = mysql_connect("localhost", "root", "");
        mysql_select_db("db");
  
        check == 0;
        $result = mysql_query("select * from account");
        $number_of_rows = mysql_num_rows($result);
        $i=0;
        $check=0;
        while ($number_of_rows != 0)
             {
                 $row =  mysql_fetch_row($result);
                 $number_of_rows--;
                 if ($username==$row[0])
                    {
                    echo "<center><br><br><br><font size = 4 color = #003366>Invalid username <BR>Currently the username has been used!!<br>";
                    echo "<BR>Click <a href="register_username.php">here</a> to try again !!<br>";
                    exit;
                    }
                    else
                    {
                    $query = "INSERT INTO account (username, password) values ('$username', '$password')";
                    mysql_query($query, $mysql_access);
                    }
             }

	check == 0;
        $result = mysql_query("select * from account1");
        $number_of_rows = mysql_num_rows($result);
        $i=0;
        $check=0;
        while ($number_of_rows != 0)
             {
                 $row =  mysql_fetch_row($result);
                 $number_of_rows--;
                 if ($username1==$row[0])
                    {
                    echo "<center><br><br><br><font size = 4 color = #003366>Invalid username <BR>Currently the username has been used!!<br>";
                    echo "<BR>Click <a href="register_username.php">here</a> to try again !!<br>";
                    exit;
                    }
                    else
                    {
                    $query = "INSERT INTO account (username, password) values ('$username', '$password')";
                    mysql_query($query, $mysql_access);
                    }
             }

Posted: Sun Aug 10, 2003 4:38 am
by JAM
From where do you get the $username and $password? Looks like the problem is elsewhere (?)

Also, some tips

Code: Select all

<?php
    // imho easier validation than you loop
    // edited as I mistyped...
    $result = mysql_query("select username from account where username = '$username'")
    if (mysql_num_rows($result) > 0) {
        echo 'account taken';
    } else {
        echo 'account available';
    }
?>

Posted: Sun Aug 10, 2003 6:37 am
by Drachlen
I'm not sure about what jam said, because hes query is missing a few things. Hes calling to a field that is a variable (which is the username, could be basically anything), which just doesn't work.. This is what i use for checking the name:

Code: Select all

$name_check = mysql_query("SELECT username FROM users WHERE username='$username'") or die(mysql_error());

if (mysql_num_rows($name_check) == 1) {
//name is taken
die();
} else {
//name is not taken
}

Posted: Sun Aug 10, 2003 6:42 am
by JAM
[quote="Drachlen"]I'm not sure about what jam said, because hes query is missing a few things. Hes calling to a field that is a variable...[quote]

Yah, youre right, was abit to fast when typing... [Edited above]

Posted: Mon Aug 11, 2003 12:10 am
by sguy
your script is work for one time only, when I register a user that the username currently not in the database, it can store in the database.
then if i back to the register page again and register another username, it always show me the name is taken.

when i register a name that currently in the database, it show me the username is taken, then i back to register page and register another username, it show me the username is taken although the username is not in the database.

Posted: Mon Aug 11, 2003 1:06 am
by JAM
Cant see the problem, or it's so obvious that I'm mising it...

You could, read the Passing Variables post (link in my signature) about using information from forms. Perhaps, just perhaps it can clear some issues (if the $username is somehow not renewed when you press back and retries).

For elseones reading, is this the IE6 bug with missing PRIVATE header?