+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 20

Thread: PHP Help

  1. #1
    Zenax's Avatar
    Zenax is offline Lord Of The Keys Zenax is an unknown quantity at this point
    Join Date
    Jul 2006
    Location
    The Brilliant United Kingdom
    Posts
    1,339

    PHP Help

    PHP Code:

    <?Php

    // including the db connection script
    require_once("connect_db.php");

    // declaring the variables
    $username $_POST['username'];
    $password $_POST['password'];
    $passrept $_POST['passrept'];

    // stripping HTML tags from the info entered
    $_POST['username'] = strip_tags($_POST['username']);
    $_POST['password'] = strip_tags($_POST['password']);
    $_POST['passrept'] = strip_tags($_POST['passrept']);

    // Checking a username is not already taken

    $SQL "SELECT * FROM users WHERE users = $username";
    $result mysql_query($SQL);
    $num_rows mysql_num_rows($result)or die(mysql_error());

    if (
    $num_rows 0) {
    $errorMessage "Username already taken";
    }
    else {
    }

    // encrypt the password into md5
    $_POST['password'] = md5($_POST['password']);

    // inserting the data into the db
    $insert mysql_query("INSERT INTO users VALUES ('"$_POST['username'] ."', '"$_POST['password'] ."') ")
        or die(
    "Could not insert data because ".mysql_error());
        
    echo (
    'Your account has been added!');



    ?>
    Currently working on a PHP registration script for my CMS. I am needing to check if a username is taken, and the tutorials I have fount online seem to follow the route highlighted in bold.

    Do help me out as that method does not work. Thanks for your help.
    Regards,
    Zenax

  2. #2
    Brandon's Avatar
    Brandon is offline Former Senior Account Rep Brandon is on a distinguished road
    Join Date
    Jun 2006
    Location
    Tewksbury, MA
    Posts
    9,589

    Re: PHP Help

    Try this, you had the data outside the else {}, so it didn't matter.

    PHP Code:
    <?Php

    // including the db connection script
    require_once("connect_db.php");

    // declaring the variables
    $username $_POST['username'];
    $password $_POST['password'];
    $passrept $_POST['passrept'];

    // stripping HTML tags from the info entered
    $_POST['username'] = strip_tags($_POST['username']);
    $_POST['password'] = strip_tags($_POST['password']);
    $_POST['passrept'] = strip_tags($_POST['passrept']);

    // Checking a username is not already taken

    $SQL "SELECT * FROM users WHERE users = $username";
    $result mysql_query($SQL);
    $num_rows mysql_num_rows($result)or die(mysql_error());

    if (
    $num_rows 0) {
    $errorMessage "Username already taken";
    }
    else {

    // encrypt the password into md5
    $_POST['password'] = md5($_POST['password']);

    // inserting the data into the db
    $insert mysql_query("INSERT INTO users VALUES ('"$_POST['username'] ."', '"$_POST['password'] ."') ")
        or die(
    "Could not insert data because ".mysql_error());
        
    echo (
    'Your account has been added!');

    }



    ?>
    Last edited by Brandon; 06-19-2007 at 03:29 PM.
    Thanks,
    Brandon Long

  3. #3
    Zenax's Avatar
    Zenax is offline Lord Of The Keys Zenax is an unknown quantity at this point
    Join Date
    Jul 2006
    Location
    The Brilliant United Kingdom
    Posts
    1,339

    Re: PHP Help

    Did i mention that this is my error message:

    Code:
    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in ..[file path here removed for security].. on line 20
    Unknown column 'test' in 'where clause'
    Regards,
    Zenax

  4. #4
    trev's Avatar
    trev is offline x10 Lieutenant trev is an unknown quantity at this point
    Join Date
    Feb 2005
    Location
    Guildford
    Posts
    334

    Re: PHP Help

    From that im guessing u can try this replace

    $SQL = "SELECT * FROM users WHERE users = $username";

    with

    $SQL = "SELECT * FROM users WHERE users = '$username'";
    Last edited by trev; 06-19-2007 at 04:48 PM.
    :ughdance: ::Trev Is back:: :ughdance:
    X10Hosting is the best free host ever

  5. #5
    Zenax's Avatar
    Zenax is offline Lord Of The Keys Zenax is an unknown quantity at this point
    Join Date
    Jul 2006
    Location
    The Brilliant United Kingdom
    Posts
    1,339

    Re: PHP Help

    ok so that got rid of the error message. however now all I get is a blank page, and the "Your account has been added" doesnt show up when I add a new account.

    Second, I don't get an error message now when i use a username that should be taken.
    Regards,
    Zenax

  6. #6
    trev's Avatar
    trev is offline x10 Lieutenant trev is an unknown quantity at this point
    Join Date
    Feb 2005
    Location
    Guildford
    Posts
    334

    Re: PHP Help

    PHP Code:
    <?php

    // including the db connection script
    require_once("connect_db.php");

    // declaring the variables
    $username $_POST['username'];
    $password $_POST['password'];
    $passrept $_POST['passrept'];

    // stripping HTML tags from the info entered
    $_POST['username'] = strip_tags($_POST['username']);
    $_POST['password'] = strip_tags($_POST['password']);
    $_POST['passrept'] = strip_tags($_POST['passrept']);

    // Checking a username is not already taken

    $q mysql_query("SELECT * FROM Users WHERE Username = '$username'") or die(mysql_error());
    if(
    mysql_num_rows($q) > 0)
    {

    echo 
    '<script>alert("The username you entered is already in use, please try again.");</script>';
    echo 
    '<script>history.back(1);</script>';
    exit;

    }
    else {

    // encrypt the password into md5
    $_POST['password'] = md5($_POST['password']);

    // inserting the data into the db
    $insert mysql_query("INSERT INTO users VALUES ('"$_POST['username'] ."', '"$_POST['password'] ."') ")
        or die(
    "Could not insert data because ".mysql_error());
        
    echo (
    'Your account has been added!');

    }



    ?>
    Try that its something from an old script i use.
    Last edited by Chris Z; 06-19-2007 at 05:24 PM. Reason: added php coloring
    :ughdance: ::Trev Is back:: :ughdance:
    X10Hosting is the best free host ever

  7. #7
    Zenax's Avatar
    Zenax is offline Lord Of The Keys Zenax is an unknown quantity at this point
    Join Date
    Jul 2006
    Location
    The Brilliant United Kingdom
    Posts
    1,339

    Re: PHP Help

    Unknown column 'user' in 'where clause'

    this be annoying. script was working fine until i included this part of the script .... added for extra security!
    Regards,
    Zenax

  8. #8
    trev's Avatar
    trev is offline x10 Lieutenant trev is an unknown quantity at this point
    Join Date
    Feb 2005
    Location
    Guildford
    Posts
    334

    Re: PHP Help

    PHP Code:
    <?Php

    // including the db connection script
    require_once("connect_db.php");

    // declaring the variables
    $username $_POST['username'];
    $password $_POST['password'];
    $passrept $_POST['passrept'];

    // stripping HTML tags from the info entered
    $_POST['username'] = strip_tags($_POST['username']);
    $_POST['password'] = strip_tags($_POST['password']);
    $_POST['passrept'] = strip_tags($_POST['passrept']);

    // Checking a username is not already taken

    $q mysql_query("SELECT * FROM users WHERE users = '$username'") or die(mysql_error());
    if(
    mysql_num_rows($q) > 0)
    {

    echo 
    '<script>alert("The username you entered is already in use, please try again.");</script>';
    echo 
    '<script>history.back(1);</script>';
    exit;

    }
    else {

    // encrypt the password into md5
    $_POST['password'] = md5($_POST['password']);

    // inserting the data into the db
    $insert mysql_query("INSERT INTO users VALUES ('"$_POST['username'] ."', '"$_POST['password'] ."') ")
        or die(
    "Could not insert data because ".mysql_error());
        
    echo (
    'Your account has been added!');

    }



    ?>
    Little tweaking, you realy do need the username check though without it people will never know when they use the same username.
    :ughdance: ::Trev Is back:: :ughdance:
    X10Hosting is the best free host ever

  9. #9
    Chris Z's Avatar
    Chris Z is offline x10 Spammer Chris Z is an unknown quantity at this point
    Join Date
    Sep 2005
    Location
    Alabama, USA
    Posts
    2,802

    Re: PHP Help

    Try making the post variables their own variables, such as:
    PHP Code:
    $PassWord md5($_POST['password']); 
    If you do this, then the insert would look like this:
    PHP Code:
    $insert mysql_query("INSERT INTO `users` (`username`, `password`) VALUES ('$UserName', '$PassWord'); 
    Last edited by Chris Z; 06-19-2007 at 05:34 PM.
    -Chris Z
    Retired Account Manager


  10. #10
    samurai1993's Avatar
    samurai1993 is offline x10Hosting Member samurai1993 is an unknown quantity at this point
    Join Date
    Sep 2006
    Location
    chile
    Posts
    26

    Re: PHP Help

    I saw that the script repeat the same code many times, you can split some code into a single line
    And I recomend to use Salt Text with the md5 hash
    You can put the salt text into your config file, for example
    Code:
    $salt  =  "putyoursalttexthere" ;
    Code:
    // declaring the variables 
    $username = strip_tags($_POST['username']); 
    $password = strip_tags(md5($salt . $_POST['password'])); 
    $passrept = strip_tags(md5($salt . $_POST['passrept'])); 
    Last edited by samurai1993; 06-19-2007 at 05:44 PM.


    :laugh: SOMOS LA HINCHADA DEL PUEBLO:laugh:
    :cool: SANTIAGO WANDERERS DE VALPARAISO!!!!:cool:

+ Reply to Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. [PHP] Variables in PHP
    By Bryon in forum Tutorials
    Replies: 15
    Last Post: 01-29-2009, 09:46 AM
  2. tons of PHP Resources
    By Chris S in forum Scripts & 3rd Party Apps
    Replies: 10
    Last Post: 01-16-2009, 10:07 AM
  3. [PHP] PHP For Starters
    By Complex in forum Tutorials
    Replies: 24
    Last Post: 06-14-2008, 11:40 PM
  4. Unstand PHP?
    By o0slowpaul0o in forum Tutorials
    Replies: 8
    Last Post: 01-07-2008, 09:16 PM
  5. "PHP Startup: Invalid Library" - Interesting error
    By javaguy78 in forum Free Hosting
    Replies: 5
    Last Post: 03-27-2007, 02:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers