register

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
sguy
Forum Commoner
Posts: 61
Joined: Sun Aug 10, 2003 2:44 am

register

Post 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);
                    }
             }
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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';
    }
?>
Last edited by JAM on Sun Aug 10, 2003 6:40 am, edited 1 time in total.
Drachlen
Forum Contributor
Posts: 153
Joined: Fri Apr 25, 2003 1:16 am

Post 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
}
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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]
User avatar
sguy
Forum Commoner
Posts: 61
Joined: Sun Aug 10, 2003 2:44 am

Post 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.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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?
Post Reply