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:
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.
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:
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
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.
$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:
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.
you know you can check his profile and see he hasn't logged in since 3 minutes after asking the question.