URGENT $20 paypal for a fix!!! Database, last 100 rows. Check if user exists

Discussion in 'PHP' started by cyclotron, Dec 5, 2010.

  1. #1
    Hey guys, got a quick PHP problem I need help with..

    I am trying to check that if the username exists already in the last 100 rows, go to the last part (you are already on train) but if username isnt in the last 100 rows, add it again. Its not working, it keeps getting the row regardless.

    $result2 = mysql_query("SELECT * FROM riders WHERE username='".$_POST['username']."' order by id desc limit 0, 100");
    
    
    	if(!mysql_num_rows($result2)){
    
    
    if($avatarurl=="") {
    	$avatarurl = "http://assets.tumblr.com/images/default_avatar_128.gif";
    }
    
    mysql_query("INSERT INTO riders (username,avatar) VALUES ('".htmlspecialchars($_POST['username'])."' , '".$avatarurl."')");
    
    header('Location: http://www.site.com/?ride=yes&mes=You+have+been+added+to+the+tumblr+follow+train.+Click+Post+train+code+below.');
     
    
    }
    
    else {
    
    header('Location: http://www.site.com/?mes=You+already+on+the+train.+You+must+wait+until+you+are+off+the+train');
    
    }
    PHP:
     
    cyclotron, Dec 5, 2010 IP
  2. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #2
    well a freebie hint would be to remove the constraint of WHERE username=...
    you are asking for the last 100 records which contain the username and of course, the username will be in that result (and only that username).
    then you have to see if that username is in that result set.
     
    shallowink, Dec 5, 2010 IP
  3. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #3

    Code not tested, but hopefully you'll get the idea.

    
    if($avatarurl=="") {
        $avatarurl = "http://assets.tumblr.com/images/default_avatar_128.gif";
    }
    
    $result = mysql_query("SELECT * FROM riders WHERE username='".$_POST['username']."' ORDER BY id DESC LIMIT 100") or die(mysql_error());
    if (mysql_num_rows($result) == 0) { // not in the last 100
       mysql_query("INSERT INTO riders (username,avatar) VALUES ('".htmlspecialchars($_POST['username'])."' , '".$avatarurl."')");
    header('Location: http://www.site.com/?ride=yes&mes=You+have+been+added+to+the+tumblr+follow+train.+Click+Post+train+code+below.');
    } else { // is in the last 100, delete it anyway and then add it again
        mysql_query("DELETE FROM riders WHERE username ='" . htmlspecialchars($_POST['username']) . "' LIMIT 1");
        mysql_query("INSERT INTO riders (username,avatar) VALUES ('".htmlspecialchars($_POST['username'])."' , '".$avatarurl."')");
    header('Location: http://www.site.com/?mes=You+already+on+the+train.+You+must+wait+until+you+are+off+the+train');
    }
    
    PHP:
     
    Last edited: Dec 5, 2010
    MyVodaFone, Dec 5, 2010 IP
  4. drctaccess

    drctaccess Peon

    Messages:
    62
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    0
    #4
    use this code:

    
    $result2 = mysql_query("SELECT * FROM riders order by id desc limit 0, 100");
    $exists = array();
    while($row = mysql_fetch_assoc($result2))
    {
            $exists[$row['id']] = $row['username'];
    }
    if(array_search($_POST['username'], $exists) == FALSE)
    {
            if($avatarurl=="")
            {
                    $avatarurl = "http://assets.tumblr.com/images/default_avatar_128.gif";
            }
            mysql_query("INSERT INTO riders (username,avatar) VALUES ('".htmlspecialchars($_POST['username'])."' , '".$avatarurl."')");
            header('Location: http://www.site.com/?ride=yes&mes=You+have+been+added+to+the+tumblr+follow+train.+Click+Post+train+code+below.');
    }
    else
    {
            header('Location: http://www.site.com/?mes=You+already+on+the+train.+You+must+wait+until+you+are+off+the+train');
    }
    
    Code (markup):
    I hope it helps
     
    drctaccess, Dec 5, 2010 IP
  5. w47w47

    w47w47 Peon

    Messages:
    255
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    you shoud count this array:

    mysql_query("SELECT * FROM riders WHERE username='".$_POST['username']."' order by id desc limit 0, 100")

    and if it's > 0 then it should add.
     
    w47w47, Dec 6, 2010 IP
  6. tvoodoo

    tvoodoo Active Member

    Messages:
    239
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    53
    #6
    
    $result2 = mysql_query("SELECT * FROM (SELECT * FROM riders ORDER BY BY ID DESC LIMIT 100) as a WHERE a.username = '".$_POST['username']."' LIMIT 1");
    
    if(mysql_num_rows($result2) == 1)
    {
    if($avatarurl=="") {
        $avatarurl = "http://assets.tumblr.com/images/default_avatar_128.gif";
    }
    
    mysql_query("INSERT INTO riders (username,avatar) VALUES ('".htmlspecialchars($_POST['username'])."' , '".$avatarurl."')");
    
    header('Location: http://www.site.com/?ride=yes&mes=You+have+been+added+to+the+tumblr+follow+train.+Click+Post+train+code+below.');
    } else {
    header('Location: http://www.site.com/?mes=You+already+on+the+train.+You+must+wait+until+you+are+off+the+train');
    }
    
    PHP:
     
    tvoodoo, Dec 6, 2010 IP
  7. w47w47

    w47w47 Peon

    Messages:
    255
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    i think that tvoodoo has it. ;) but i didn't tested... don't know what mysql_num_rows returns on false. haven't been working on mysql for a long time now... only php + other db.
     
    w47w47, Dec 6, 2010 IP
  8. tvoodoo

    tvoodoo Active Member

    Messages:
    239
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    53
    #8
    mysql_num_rows returns false when the query fails or it will return the number of rows on success.
     
    tvoodoo, Dec 6, 2010 IP
  9. glaizagarcia

    glaizagarcia Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    @cyclotron : sent you a PM.. pls check
     
    glaizagarcia, Dec 7, 2010 IP
  10. glaizagarcia

    glaizagarcia Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    @cyclotron : have you receive my code? does it help?
     
    glaizagarcia, Dec 13, 2010 IP
  11. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #11
    you know you can check his profile and see he hasn't logged in since 3 minutes after asking the question.
     
    shallowink, Dec 13, 2010 IP