SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2009
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    user registration script

    hi

    i am trying to register new users by having users inputting a username and password into a database called test and a table called members that has columns: username and password.

    but when the password and username are inserted the following message appears.

    Notice: Undefined variable: table in C:\wamp\www\website\register.php on line 28
    Could not insert data because You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'values ('tom', 'tom')' at line 1

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Assuming your getting your username and password from a form via post, your SQL should look similar to the following...
    PHP Code:
    <?php
    $sSQL 
    sprintf("INSERT INTO members (username, password)VALUES('%s', '%s')",
        
    mysql_real_escape_string($_POST['username']),
        
    mysql_real_escape_string($_POST['password'])
    );
    $rResult mysql_query($sSQL);
    ?>
    Of course, in reality you should be implementing some hashing mechanism on the users password.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2009
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i will try it

  4. #4
    SitePoint Enthusiast
    Join Date
    Feb 2009
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks SilverB it works. SUPERB!

  5. #5
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Here you go, this should put you on the right path...

    PHP Code:
    <?php
    //--> Credentials.
    $aDatabaseCredentials = array();
    $aDatabaseCredentials['host'] = 'localhost';
    $aDatabaseCredentials['name'] = 'test';
    $aDatabaseCredentials['user'] = 'john';
    $aDatabaseCredentials['pass'] = 'john';

    //--> Connection
    $rConnection mysql_connect(
        
    $aDatabaseCredentials['host'],
        
    $aDatabaseCredentials['user'],
        
    $aDatabaseCredentials['pass']
    ) or die(
    'A fatal error has occured connecting to the database.');

    //--> Database selection.
    if(!mysql_select_db($aDatabaseCredentials['name'], $rConnection))
    {
        die(
    'A fatal error has occured selecting the database.');
    }

    //--> Username check.
    $sSQL sprintf("SELECT id FROM members WHERE username = '%s' LIMIT 1",
        
    mysql_real_escape_string($_POST['username'], $rConnection)
    );
    $rResult mysql_query($sSQL$rConnection);
    if(
    mysql_num_rows($rResult) !== 1)
    {
        echo 
    'Sorry, that username already exists.';
        exit;
    }

    //--> New user creation.
    $sSQL sprintf("INSERT INTO members (username, password)VALUES('%s', '%s')",
        
    mysql_real_escape_string($_POST['username'], $rConnection),
        
    mysql_real_escape_string($_POST['password'], $rConnection)
    );
    $rResult mysql_query($sSQL$rConnection);
    if(
    mysql_affected_rows($rResult$rConnection) === 1)
    {
        echo 
    'Thank you, your account has now been created.';
    }
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  6. #6
    SitePoint Enthusiast
    Join Date
    Feb 2009
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    is there any way of recalling the username after a sucessful login to give a private greeting e.g. "welcome Tony".

    That is if the username is tony.

  7. #7
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Sure.
    PHP Code:
    <?php
    $sSQL 
    sprintf("SELECT username FROM members WHERE username = '' AND password = ''",
        
    mysql_real_escape_string($_POST['username'], $rConnection),
        
    mysql_real_escape_string($_POST['password'], $rConnection)
    );
    $rResult mysql_query($sSQL$rConnection);
    $aUserData mysql_fetch_assoc($rResult);
    printf('Welcome back %s, we are glad to see you again.',
        
    $aUserData['username']
    );
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  8. #8
    SitePoint Enthusiast
    Join Date
    Feb 2009
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks SilverB.

    i have tried the code but whenever a username is inserted that is already taken up it displays: Sorry, there the username john is already taken up.
    This message occurs even when the username inserted is not john.

    Is there anyway of correcting this and picking up the username entered rather than just john. The code i am using is:
    echo "Sorry, there the username $username is already taken.<br>";

    I think its beacuse of the code at the start:

    <?php//--> Credentials.$aDatabaseCredentials = array();$aDatabaseCredentials['host'] = 'localhost';$aDatabaseCredentials['name'] = 'test';$aDatabaseCredentials['user'] = 'john';$aDatabaseCredentials['pass'] = 'john';

  9. #9
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Email? Bah, you'll have more chance finding me on here. In fact, the wife now post's when my tea's done.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  10. #10
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SilverBulletUK View Post
    Email? Bah, you'll have more chance finding me on here. In fact, the wife now post's when my tea's done.
    rofl


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •