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

Thread: PHP registration script - Username check does not work

  1. #1
    as4s1n's Avatar
    as4s1n is offline x10 Sophmore as4s1n is an unknown quantity at this point
    Join Date
    Apr 2009
    Location
    Washington State
    Posts
    174

    PHP registration script - Username check does not work

    I wrote a script for a registration page and all of a sudden it just stopped working. Whenever I try something it either reads "Username is taken" even when it isn't and if it doesn't show anything I get either an error (on a separate file) that tells me "Invalid parameters, incorrect number of tokens" or "Unknown column $name in where clause" the $name being replaced by the value. I am completely lost.

    PHP Code:
    $info = array(
    ":name"=>$_POST['name'],
    ":pass"=>md5($_POST['password']),
    ":email"=>$_POST['email'],
    ":gender"=>$_POST['gender'],
    ":age"=>$_POST['age'],
    ":location"=>$_POST['location'],
    ":avatar"=>$_FILES['avatar']['name'],
    ":avatarSize"=>$_FILES['avatar']['size']); 
    # ...
     
     
    try {
    $sth $dbh->prepare("SELECT username as numrows FROM users WHERE username = :name");
    $sth->execute($info);
    } catch(
    PDOException $e) {
    writeError($e->getMessage(),CURPAGE);
    }
    if(
    $sth->rowcount() > 0)
     
    $error[] = "<b>Username is taken</b> "
    Please help.
    There is no such thing as a "stupid question," there are only "stupid people" who don't ask them.

  2. #2
    lemon-tree's Avatar
    lemon-tree is offline x10 Minion lemon-tree has a spectacular aura about
    Join Date
    Nov 2007
    Posts
    1,420

    Re: PHP registration script - Username check does not work

    Try getting rid of 'as numrows'. I don't use think you can insert your :name variable like that either, but I may be wrong.

  3. #3
    as4s1n's Avatar
    as4s1n is offline x10 Sophmore as4s1n is an unknown quantity at this point
    Join Date
    Apr 2009
    Location
    Washington State
    Posts
    174

    Re: PHP registration script - Username check does not work

    Still the same problem. Misson always tells me to do it like that but just to be safe I changed it to this:

    PHP Code:
    $sth $dbh->prepare("SELECT id FROM users WHERE username = ?");
    $sth->execute(array($_POST['name'])); 
    Still the same problem
    There is no such thing as a "stupid question," there are only "stupid people" who don't ask them.

  4. #4
    Hello71 is offline x10Hosting Member Hello71 is an unknown quantity at this point
    Join Date
    Jan 2008
    Posts
    14

    Re: PHP registration script - Username check does not work

    Isn't the correct code
    PHP Code:
    $sth->execute("s", array($_POST['name'])); 
    ?

  5. #5
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: PHP registration script - Username check does not work

    Quote Originally Posted by as4s1n View Post
    Whenever I try something it either reads "Username is taken" even when it isn't and if it doesn't show anything I get either an error [...] or "Unknown column $name in where clause" the $name being replaced by the value. I am completely lost.
    Print the statement (with PDOStatement::debugDumpParams) and the parameter values. Try the query in phpMyAdmin. Also print the results of PDOStatement::errorInfo.

    Quote Originally Posted by as4s1n View Post
    and if it doesn't show anything I get either an error (on a separate file) that tells me "Invalid parameters, incorrect number of tokens"
    PDOStatement::execute doesn't like getting more values than there are parameters. That's probably the source of this error. In this instance, using a positional parameter is fine. If there are many parameters and the input parameter array is assembled elsewhere, you can use array_intersect_key to extract only the parameters the query needs.


    Quote Originally Posted by as4s1n View Post
    Misson always tells me to do it like that
    Not always. For queries with just a few arguments where the parameter array is constructed near the definition of the statement, positional parameters are fine. Named parameters are for when there are numerous parameters (so you can keep them straight) or when the input parameter array is assembled far from the statement, so that you can't readily look at the query and see the parameter order.


    Quote Originally Posted by Hello71 View Post
    Isn't the correct code
    PHP Code:
    $sth->execute("s", array($_POST['name'])); 
    ?
    PDOStatement::execute doesn't need to be told types for the parameters. You might be thinking of mysqli::bind_param.
    Last edited by misson; 04-26-2010 at 11:04 PM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  6. #6
    as4s1n's Avatar
    as4s1n is offline x10 Sophmore as4s1n is an unknown quantity at this point
    Join Date
    Apr 2009
    Location
    Washington State
    Posts
    174

    Re: PHP registration script - Username check does not work

    Here is what I get if I use my first section of code:
    SQL: [45] SELECT id FROM users WHERE username = ':name' Params: 8 Key: Name: [5] :name paramno=-1 name=[5] ":name" is_param=1 param_type=2 Key: Name: [5] :pass paramno=-1 name=[5] ":pass" is_param=1 param_type=2 Key: Name: [6] :email paramno=-1 name=[6] ":email" is_param=1 param_type=2 Key: Name: [7] :gender paramno=-1 name=[7] ":gender" is_param=1 param_type=2 Key: Name: [4] :age paramno=-1 name=[4] ":age" is_param=1 param_type=2 Key: Name: [9] :location paramno=-1 name=[9] ":location" is_param=1 param_type=2 Key: Name: [7] :avatar paramno=-1 name=[7] ":avatar" is_param=1 param_type=2 Key: Name: [11] :avatarSize paramno=-1 name=[11] ":avatarSize" is_param=1 param_type=2
    And if I use the second set:
    SQL: [39] SELECT id FROM users WHERE username = ? Params: 1 Key: Position #0: paramno=0 name=[0] "" is_param=1 param_type=2
    @Misson: If I used the array_intersect_key() method you talked about, which two arrays would I be comparing? Would it be something like:

    PHP Code:
    $sth->prepare("... :name");
    $sth->execute(array_intersect_key($info,something)); 
    Also, was that the right way to do the :key? Or do I have to place ' 's around it to turn it into a string?

    I.E.:
    PHP Code:
    $sth $dbh->prepare("... WHERE username = :name"); 
    Edit 1: I changed it a little bit, now I get a blank screen when I try.

    PHP Code:
    $info = array(
    ":name"=>$_POST['name'],
    ":pass"=>md5($_POST['password']),
    ":email"=>$_POST['email'],
    ":gender"=>$_POST['gender'],
    ":age"=>$_POST['age'],
    ":location"=>$_POST['location'],
    ":avatar"=>$_FILES['avatar']['name'],
    ":avatarSize"=>$_FILES['avatar']['size']);

    #...
     
    try {
    $sth $dbh->prepare("SELECT username FROM users WHERE username = ?");
    $sth->execute(array($_POST['name']));
    } catch(
    PDOException $e) {
    $sth->errorInfo();
    $sth->debugDumpParams();
    writeError($e->getMessage(),CURPAGE);
    }

    if(
    $sth->rowcount() > 0)
        
    $error[] = "<b>Username is taken</b> "
    Last edited by as4s1n; 04-27-2010 at 12:25 PM.
    There is no such thing as a "stupid question," there are only "stupid people" who don't ask them.

  7. #7
    docttor is offline x10Hosting Member docttor is an unknown quantity at this point
    Join Date
    Jan 2010
    Posts
    1

    Re: PHP registration script - Username check does not work

    PHP Code:
    $info = array( 
    $name $_POST['name'], 
    $pass md5($_POST['password']), 
    $email $_POST['email'], 
    $gender $_POST['gender'], 
    $age $_POST['age'], 
    $location $_POST['location'], 
    $avatar $_FILES['avatar']['name'], 
    $avatarSize $_FILES['avatar']['size']); 


     try { 
    $sth $dbh->prepare("SELECT username FROM users WHERE username = '{$name}' "); 
    $sth->execute(array($_POST['name'])); 
    } catch(
    PDOException $e) { 
    $sth->errorInfo(); 
    $sth->debugDumpParams(); 
    writeError($e->getMessage(),CURPAGE); 


    if(
    $sth->rowcount() > 0
        
    $error[] = "<b>Username is taken</b> "

    Maybe now will work

  8. #8
    as4s1n's Avatar
    as4s1n is offline x10 Sophmore as4s1n is an unknown quantity at this point
    Join Date
    Apr 2009
    Location
    Washington State
    Posts
    174

    Re: PHP registration script - Username check does not work

    Thanks, it works now... Now can you explain how that works? You defined a variable inside the array variable. Is that correct. And why do you need to use {$name} for that? Also, how do I call those variables onto the script? Is it $info['$name'] or something?
    Last edited by as4s1n; 04-27-2010 at 03:43 PM.
    There is no such thing as a "stupid question," there are only "stupid people" who don't ask them.

  9. #9
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: PHP registration script - Username check does not work

    Quote Originally Posted by as4s1n View Post
    Here is what I get if I use my first section of code:
    SQL: [45] SELECT id FROM users WHERE username = ':name' Params: 8
    From this, we see that you're quoting the parameter (don't) and passing too many parameter values. One is defined, you pass eight.

    Quote Originally Posted by as4s1n View Post
    @Misson: If I used the array_intersect_key() method you talked about, which two arrays would I be comparing?
    You should be able to figure this one out. You only want certain entries from the input array, those that are named parameters. Thus you intersect the input array and an array defining which keys:

    PHP Code:
        $sth->execute(array(array_intersect_key($info, array(':name' => null))); 
    However, in this case, using a single positional parameter is simple and clear, so that's what you should do.

    Another alternative (not to be used in this case) is to use PDOStatement::bindParam or PDOStatement::bindValue, then call execute() with no arguments. bindParam is particularly useful when executing the query multiple times and the value is always stored in the same variable; you can bind the parameter once and simply change what's stored in the variable.

    Quote Originally Posted by as4s1n View Post
    Also, was that the right way to do the :key? Or do I have to place ' 's around it to turn it into a string?
    Parameters are unquoted, just like variables are unquoted in other languages (you wouldn't write strtolower('$action'), would you?). Again, the documentation covers this; take a closer look at it. Remember, the point of using quotes when you interpolate a value is to delineate the embedded data, to separate it from the host language.

    Quote Originally Posted by as4s1n View Post
    Edit 1: I changed it a little bit, now I get a blank screen when I try.
    The fragment you posted only outputs something when there's an error. Perhaps the blank screen is an indication that the query succeeded, and the user name is available. Print something (e.g. "Username $info[:name] is available.") on success so you can tell.

    The call to PDOStatement::rowCount should be within the try block. If an exception is thrown, $sth won't hold valid results, and any calls on it that deal with results are meaningless. You need to consider what's valid and what isn't when thinking about errors.





    Quote Originally Posted by docttor View Post
    PHP Code:
    $sth $dbh->prepare("SELECT username FROM users WHERE username = '{$name}' "); 
    One of the two main points behind prepared statements is to avoid doing just this, in order to prevent SQL injection. You need to study more.


    Quote Originally Posted by as4s1n View Post
    You defined a variable inside the array variable. Is that correct. [...] Also, how do I call those variables onto the script? Is it $info['$name'] or something?
    $name = 'value' is an expression (i.e. it has a value), distinguished from statements (e.g. if statements), which don't have values (read "Expressions vs Statements" and "Expression Versus Statement" for more). It's the value of the expression that gets stored in the array, where indices get assigned as all integer indices are assigned. You'd refer to it as $info[0].

    Docttor uses in the same way as:

    PHP Code:
    $db->prepare($stmt "SELECT ...");

    // Note: assignment, not comparison
    while (($row $query->fetch())) {
        ...
    }
    /* Note this is better written as:
        foreach ($query->fetch() as $row) {
    but it serves to illustrate the use of assignment expressions.
    */ 
    Quote Originally Posted by as4s1n View Post
    And why do you need to use {$name} for that?
    The braces are for interpolating "complex" expressions, such as $foo->bar->baz(). In this case they're not necessary, as $name isn't a "complex" expression, but they do make it stand out more in the string.
    Last edited by misson; 04-27-2010 at 04:41 PM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  10. #10
    as4s1n's Avatar
    as4s1n is offline x10 Sophmore as4s1n is an unknown quantity at this point
    Join Date
    Apr 2009
    Location
    Washington State
    Posts
    174

    Re: PHP registration script - Username check does not work

    You should be able to figure this one out. You only want certain entries from the input array, those that are named parameters. Thus you intersect the input array and an array defining which keys:

    PHP Code:
        $sth->execute(array(array_intersect_key($info, array(':name' => null))); 
    That would probably be more than I would want to write, I would simplify it to this
    PHP Code:
    $sth->execute(array($_POST['name'])); 
    Thanks for the help, it finally works without flaw.
    There is no such thing as a "stupid question," there are only "stupid people" who don't ask them.

+ Reply to Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. registration doesent work
    By mauroca in forum Free Hosting
    Replies: 2
    Last Post: 02-03-2010, 10:13 AM
  2. PHP Registration Script Not Working
    By masterjake in forum Programming Help
    Replies: 2
    Last Post: 09-16-2008, 01:50 PM
  3. smf registration for and login script for 300 points
    By nahsorhseda in forum The Marketplace
    Replies: 0
    Last Post: 06-14-2008, 05:07 AM
  4. PHP - Registration/Login Script Request
    By masterjake in forum Programming Help
    Replies: 5
    Last Post: 01-22-2008, 12:41 AM
  5. How to check mysql username and password
    By Tideas in forum Free Hosting
    Replies: 5
    Last Post: 08-30-2007, 09:45 AM

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