1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Pdo Query Driving Me Nuts

Discussion in 'Databases' started by scottlpool2003, Jan 24, 2013.

  1. #1
    I think its something obvious but I can't see it. Not really worked with PDO before so its a little bit of a step up from the old style queries but here's my code:

    
     
    $sth = $dbconn->prepare("SELECT id, username, useremail FROM users WHERE username = '$_POST[username]'");
    $sth->execute();
     
    $result = $sth->fetch(PDO::FETCH_ASSOC);
    if ($username != '$result[username]' || $useremail != '$result[useremail]'){
    print ("Usernames/Password is the same");
    // Do stuff
    }else {
    print ("User is new!");
    //Do stuff
    }
    
    PHP:
    The user is always new...
     
    scottlpool2003, Jan 24, 2013 IP
  2. scottlpool2003

    scottlpool2003 Well-Known Member

    Messages:
    1,708
    Likes Received:
    49
    Best Answers:
    9
    Trophy Points:
    150
    #2
    Solved:

    $sth = $dbconn->prepare("SELECT * FROM users WHERE username = :username AND password = :password");
     
    $params = array("username" => $_POST["username"], "password" => $encryptedpassword);
    $sth->execute($params);
     
     
        while ($row = $sth->fetch()) {
        //do stuff
        }
    PHP:
     
    scottlpool2003, Jan 25, 2013 IP
  3. tiamak

    tiamak Active Member

    Messages:
    81
    Likes Received:
    2
    Best Answers:
    3
    Trophy Points:
    58
    #3
    imho dont use prepare/execute
    use just query instead
    
    $sth = $dhconn->query('SELECT * FROM users WHERE username = "'.$_POST['username'].'" AND password = "'.$encryptedpassword.'"');
    while ($row = $sth->fetch(PDO::FETCH_ASSOC,PDO::FETCH_ORI_NEXT)) {
            //do stuff
    }
    
    PHP:
    it is much shorter :)
     
    tiamak, Feb 1, 2013 IP
  4. NLZ13

    NLZ13 Well-Known Member

    Messages:
    166
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    113
    #4
    You shouldn't do it like that, espacially don't put POST parameters directly into a query.
    A hacker will need a minutes to hack your website like that. You might want to look into "sql injection" on google
     
    Last edited: Feb 1, 2013
    NLZ13, Feb 1, 2013 IP
  5. tiamak

    tiamak Active Member

    Messages:
    81
    Likes Received:
    2
    Best Answers:
    3
    Trophy Points:
    58
    #5
    but prepare doesnt protect u against sql injection ;)
    using "'.$_POST['foo'].'" is better however far from being perfect.
    I usually embed $_POST/$_GET into custom function that checks data or just add (int) for numbers.
     
    tiamak, Feb 1, 2013 IP
  6. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #6
    EricBruggema, Feb 2, 2013 IP
  7. scottlpool2003

    scottlpool2003 Well-Known Member

    Messages:
    1,708
    Likes Received:
    49
    Best Answers:
    9
    Trophy Points:
    150
    #7
    Just to update the code for this thread:

    $sth = $dbconn->prepare("SELECT * FROM users WHERE username = :username AND password = :password");
     
    $params = array("username" => $_POST["username"], "password" => $encryptedpassword);
    $sth->execute($params);
     
     
        while ($row = $sth->fetch()) {
       
        if ($row[active] == 1){
     
        // user is active proceed to account
       
        session_start();
        $_SESSION['userName'] ="$row[username]";
        header("Location:/account/");
        }
     
        else {
       
        // user is inactive show error
     
    }
    PHP:
    As pointed out earlier, posting the variables directly into the query is dangerous. There's various other methods to bind them such as the example above as PDO only protects against injections if the parameters are binded.
     
    scottlpool2003, Feb 4, 2013 IP
  8. tiamak

    tiamak Active Member

    Messages:
    81
    Likes Received:
    2
    Best Answers:
    3
    Trophy Points:
    58
    #8
    please read a little more that this one manual page.

    How about PDO::ATTR_EMULATE_PREPARES ;) ??

    in order to prevent sql injection you would need to use not only prepare but also set

    PDO::ATTR_EMULATE_PREPARES to false and/or use bindParam, bindValue

    Simple prepare and then execute is NOT preventing You against sql injection.
    If You dont belive please do reaserch and see Yourself.
     
    tiamak, Feb 4, 2013 IP