spyke01 Posted October 16, 2005 Share Posted October 16, 2005 hi everyone, im running a usersonline script, but it keeps giving me problems really bad, ive asked several times in the php forums, and now its time to ask here, because i think its something to do with the way im using the table in the database, you can see the script in action here: http://www.spyke01.mfhosting.com/forum/indexnew.php the error im getting is Error, update query failed UPDATE `usersonline` SET username='spyke01', timestamp='1129482508', ip='71.113.253.198', file='/forum/index.php' WHERE ip='71.113.253.198' OR username='spyke01' when i run it in phpmyadmin, i get: #1062 - Duplicate entry '71.113.253.198' for key 1 the database is this: CREATE TABLE `usersonline` ( `username` varchar(25) NOT NULL default '', `timestamp` int(15) NOT NULL default '0', `ip` varchar(40) NOT NULL default '', `file` varchar(100) NOT NULL default '', PRIMARY KEY (`ip`) ) TYPE=MyISAM; the actual code for this page is: [!--PHP-Head--][div class=\'phptop\']PHP[/div][div class=\'phpmain\'][!--PHP-EHead--] [span style=\"color:#0000BB\"]<?php $timeoutseconds [/span][span style=\"color:#007700\"]= ([/span][span style=\"color:#0000BB\"]$_SESSION[/span][span style=\"color:#007700\"][[/span][span style=\"color:#DD0000\"]\'username\'[/span][span style=\"color:#007700\"]]) ? [/span][span style=\"color:#DD0000\"]\'2592000\' [/span][span style=\"color:#007700\"]: [/span][span style=\"color:#DD0000\"]\'1200\'[/span][span style=\"color:#007700\"]; [/span][span style=\"color:#0000BB\"]$timestamp[/span][span style=\"color:#007700\"]=[/span][span style=\"color:#0000BB\"]time[/span][span style=\"color:#007700\"](); [/span][span style=\"color:#0000BB\"]$timeout[/span][span style=\"color:#007700\"]=[/span][span style=\"color:#0000BB\"]$timestamp[/span][span style=\"color:#007700\"]+[/span][span style=\"color:#0000BB\"]$timeoutseconds[/span][span style=\"color:#007700\"]; [/span][span style=\"color:#0000BB\"]$ip [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#0000BB\"]$REMOTE_ADDR[/span][span style=\"color:#007700\"]; [/span][span style=\"color:#0000BB\"]$currentuser [/span][span style=\"color:#007700\"]= ([/span][span style=\"color:#0000BB\"]$_SESSION[/span][span style=\"color:#007700\"][[/span][span style=\"color:#DD0000\"]\'username\'[/span][span style=\"color:#007700\"]]) ? [/span][span style=\"color:#0000BB\"]$_SESSION[/span][span style=\"color:#007700\"][[/span][span style=\"color:#DD0000\"]\'username\'[/span][span style=\"color:#007700\"]] : [/span][span style=\"color:#DD0000\"]\'Guest\'[/span][span style=\"color:#007700\"]; [/span][span style=\"color:#FF8000\"]// Add this user to database [/span][span style=\"color:#0000BB\"]$sql [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#DD0000\"]\"SELECT * FROM `usersonline` WHERE username = \'\" [/span][span style=\"color:#007700\"]. [/span][span style=\"color:#0000BB\"]$_SESSION[/span][span style=\"color:#007700\"][[/span][span style=\"color:#DD0000\"]\'username\'[/span][span style=\"color:#007700\"]] . [/span][span style=\"color:#DD0000\"]\"\' OR ip = \'$ip\'\"[/span][span style=\"color:#007700\"]; [/span][span style=\"color:#0000BB\"]$result [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#0000BB\"]mysql_query[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$sql[/span][span style=\"color:#007700\"]) or die ([/span][span style=\"color:#0000BB\"]mysql_error[/span][span style=\"color:#007700\"]().[/span][span style=\"color:#DD0000\"]\"<br />Couldn\'t execute query: $sql\"[/span][span style=\"color:#007700\"]); if([/span][span style=\"color:#0000BB\"]mysql_num_rows[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$result[/span][span style=\"color:#007700\"]) == [/span][span style=\"color:#0000BB\"]0[/span][span style=\"color:#007700\"]) [/span][span style=\"color:#FF8000\"]//if NO results, add this user [/span][span style=\"color:#007700\"]{ [/span][span style=\"color:#FF8000\"]//not in db, so add them [/span][span style=\"color:#0000BB\"]$sql2 [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#DD0000\"]\"INSERT INTO `usersonline` (username, timestamp, ip, file)\"[/span][span style=\"color:#007700\"]. [/span][span style=\"color:#DD0000\"]\"VALUES(\'$currentuser\', \'$timestamp\', \'$ip\', \'\"[/span][span style=\"color:#007700\"].[/span][span style=\"color:#0000BB\"]$_SERVER[/span][span style=\"color:#007700\"][[/span][span style=\"color:#DD0000\"]\'PHP_SELF\'[/span][span style=\"color:#007700\"]].[/span][span style=\"color:#DD0000\"]\"\')\"[/span][span style=\"color:#007700\"]; [/span][span style=\"color:#0000BB\"]mysql_query[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$sql2[/span][span style=\"color:#007700\"]) or die([/span][span style=\"color:#DD0000\"]\'<br />Error, insert query failed\' [/span][span style=\"color:#007700\"]. [/span][span style=\"color:#0000BB\"]$sql2[/span][span style=\"color:#007700\"]); } else [/span][span style=\"color:#FF8000\"]//if result found, run the rest of the script [/span][span style=\"color:#007700\"]{ [/span][span style=\"color:#FF8000\"]//in db, so update their info [/span][span style=\"color:#0000BB\"]$sql2 [/span][span style=\"color:#007700\"]= ([/span][span style=\"color:#0000BB\"]$_SESSION[/span][span style=\"color:#007700\"][[/span][span style=\"color:#DD0000\"]\'username\'[/span][span style=\"color:#007700\"]]) ? [/span][span style=\"color:#DD0000\"]\"UPDATE `usersonline` SET username=\'$currentuser\', timestamp=\'$timestamp\', ip=\'$ip\', file=\'\" [/span][span style=\"color:#007700\"]. [/span][span style=\"color:#0000BB\"]$_SERVER[/span][span style=\"color:#007700\"][[/span][span style=\"color:#DD0000\"]\'PHP_SELF\'[/span][span style=\"color:#007700\"]] . [/span][span style=\"color:#DD0000\"]\"\' WHERE ip=\'$ip\' OR username=\'$currentuser\'\" [/span][span style=\"color:#007700\"]: [/span][span style=\"color:#DD0000\"]\"UPDATE `usersonline` SET username=\'$currentuser\', timestamp=\'$timestamp\', ip=\'$ip\', file=\'\" [/span][span style=\"color:#007700\"]. [/span][span style=\"color:#0000BB\"]$_SERVER[/span][span style=\"color:#007700\"][[/span][span style=\"color:#DD0000\"]\'PHP_SELF\'[/span][span style=\"color:#007700\"]] . [/span][span style=\"color:#DD0000\"]\"\' WHERE ip=\'$ip\'\"[/span][span style=\"color:#007700\"]; [/span][span style=\"color:#0000BB\"]mysql_query[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$sql2[/span][span style=\"color:#007700\"]) or die([/span][span style=\"color:#DD0000\"]\'<br />Error, update query failed\' [/span][span style=\"color:#007700\"]. [/span][span style=\"color:#0000BB\"]$sql2[/span][span style=\"color:#007700\"]); } [/span][span style=\"color:#0000BB\"]mysql_free_result[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$result[/span][span style=\"color:#007700\"]); [/span][span style=\"color:#FF8000\"]//free our query // Delete users that have been online for more then \"$timeoutseconds\" seconds [/span][span style=\"color:#007700\"]@[/span][span style=\"color:#0000BB\"]mysql_query[/span][span style=\"color:#007700\"]([/span][span style=\"color:#DD0000\"]\"delete from useronline where timestamp>$timeout\"[/span][span style=\"color:#007700\"]); [/span][span style=\"color:#FF8000\"]// Shows total users online as well as username [/span][span style=\"color:#0000BB\"]$sql [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#DD0000\"]\"SELECT * FROM `usersonline`\"[/span][span style=\"color:#007700\"]; [/span][span style=\"color:#0000BB\"]$result [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#0000BB\"]mysql_query[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$sql[/span][span style=\"color:#007700\"]) or die ([/span][span style=\"color:#0000BB\"]mysql_error[/span][span style=\"color:#007700\"]().[/span][span style=\"color:#DD0000\"]\"<br />Couldn\'t execute query: $sql\"[/span][span style=\"color:#007700\"]); if([/span][span style=\"color:#0000BB\"]mysql_num_rows[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$result[/span][span style=\"color:#007700\"]) == [/span][span style=\"color:#0000BB\"]0[/span][span style=\"color:#007700\"]) [/span][span style=\"color:#FF8000\"]//if NO results, stop the script & return the error message [/span][span style=\"color:#007700\"]{ echo [/span][span style=\"color:#DD0000\"]\"\nError reading usersonline table section 2.<br /><br />\"[/span][span style=\"color:#007700\"]; } else [/span][span style=\"color:#FF8000\"]//if result found, run the rest of the script [/span][span style=\"color:#007700\"]{ [/span][span style=\"color:#0000BB\"]$totalonline [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#0000BB\"]mysql_num_rows[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$result[/span][span style=\"color:#007700\"]); } [/span][span style=\"color:#0000BB\"]mysql_free_result[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$result[/span][span style=\"color:#007700\"]); [/span][span style=\"color:#FF8000\"]//free our query //Find out whos online and if they\'re a user, make a link to their profile page [/span][span style=\"color:#0000BB\"]$sql [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#DD0000\"]\"SELECT * FROM `usersonline` WHERE username != \'Guest\'\"[/span][span style=\"color:#007700\"]; [/span][span style=\"color:#0000BB\"]$result [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#0000BB\"]mysql_query[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$sql[/span][span style=\"color:#007700\"]) or die ([/span][span style=\"color:#0000BB\"]mysql_error[/span][span style=\"color:#007700\"]().[/span][span style=\"color:#DD0000\"]\"<br />Couldn\'t execute query: $sql\"[/span][span style=\"color:#007700\"]); [/span][span style=\"color:#0000BB\"]$totalguests [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#0000BB\"]$totalonline [/span][span style=\"color:#007700\"]- [/span][span style=\"color:#0000BB\"]mysql_num_rows[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$result[/span][span style=\"color:#007700\"]); [/span][span style=\"color:#0000BB\"]$totalusers [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#0000BB\"]$totalonline [/span][span style=\"color:#007700\"]- [/span][span style=\"color:#0000BB\"]$totalguests[/span][span style=\"color:#007700\"]; if([/span][span style=\"color:#0000BB\"]mysql_num_rows[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$result[/span][span style=\"color:#007700\"]) == [/span][span style=\"color:#0000BB\"]0[/span][span style=\"color:#007700\"]) [/span][span style=\"color:#FF8000\"]//if NO results, stop the script & return the error message [/span][span style=\"color:#007700\"]{ [/span][span style=\"color:#0000BB\"]$users [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#DD0000\"]\"No registered users logged in.\"[/span][span style=\"color:#007700\"]; [/span][span style=\"color:#FF8000\"]//add username to our list ^^ [/span][span style=\"color:#007700\"]} else [/span][span style=\"color:#FF8000\"]//if result found, run the rest of the script [/span][span style=\"color:#007700\"]{ while ( [/span][span style=\"color:#0000BB\"]$row [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#0000BB\"]mysql_fetch_array[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$result[/span][span style=\"color:#007700\"]) ) { [/span][span style=\"color:#0000BB\"]$sql2 [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#DD0000\"]\"SELECT * FROM `users` WHERE username = \'$row[username]\'\"[/span][span style=\"color:#007700\"]; [/span][span style=\"color:#0000BB\"]$result2 [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#0000BB\"]mysql_query[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$sql2[/span][span style=\"color:#007700\"]) or die ([/span][span style=\"color:#0000BB\"]mysql_error[/span][span style=\"color:#007700\"]().[/span][span style=\"color:#DD0000\"]\"<br />Couldn\'t execute query: $sql\"[/span][span style=\"color:#007700\"]); if([/span][span style=\"color:#0000BB\"]mysql_num_rows[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$result2[/span][span style=\"color:#007700\"]) == [/span][span style=\"color:#0000BB\"]0[/span][span style=\"color:#007700\"]) [/span][span style=\"color:#FF8000\"]//if NO results, stop the script & return the error message [/span][span style=\"color:#007700\"]{ [/span][span style=\"color:#0000BB\"]$users [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#DD0000\"]\"No registered users logged in.\"[/span][span style=\"color:#007700\"]; [/span][span style=\"color:#FF8000\"]//add username to our list ^^ [/span][span style=\"color:#007700\"]} else [/span][span style=\"color:#FF8000\"]//if result found, run the rest of the script [/span][span style=\"color:#007700\"]{ while ( [/span][span style=\"color:#0000BB\"]$row2 [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#0000BB\"]mysql_fetch_array[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$result2[/span][span style=\"color:#007700\"]) ) { [/span][span style=\"color:#0000BB\"]$users [/span][span style=\"color:#007700\"].= [/span][span style=\"color:#DD0000\"]\"<a href=\'$menuvar[PROFILE]&action=viewprofile&id=\" [/span][span style=\"color:#007700\"]. [/span][span style=\"color:#0000BB\"]$row2[/span][span style=\"color:#007700\"][[/span][span style=\"color:#DD0000\"]\'userid\'[/span][span style=\"color:#007700\"]] . [/span][span style=\"color:#DD0000\"]\"\'>$row2[username]</a> \"[/span][span style=\"color:#007700\"]; } } [/span][span style=\"color:#0000BB\"]mysql_free_result[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$result2[/span][span style=\"color:#007700\"]); [/span][span style=\"color:#FF8000\"]//free our query [/span][span style=\"color:#007700\"]} } [/span][span style=\"color:#0000BB\"]mysql_free_result[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$result[/span][span style=\"color:#007700\"]); [/span][span style=\"color:#FF8000\"]//free our query //Print out our nice table [/span][span style=\"color:#007700\"]echo [/span][span style=\"color:#DD0000\"]\"\n<center>\"[/span][span style=\"color:#007700\"]; echo [/span][span style=\"color:#DD0000\"]\"\n<table class=\'forumborder\' border=\'0\' cellpadding=\'0\' cellspacing=\'1\' width=\'600\'>\"[/span][span style=\"color:#007700\"]; echo [/span][span style=\"color:#DD0000\"]\"\n <tr class=\'title2\' nowrap height=\'10\'>\"[/span][span style=\"color:#007700\"]; echo [/span][span style=\"color:#DD0000\"]\"\n <td><center><b>Online Stats</b></center></td>\"[/span][span style=\"color:#007700\"]; echo [/span][span style=\"color:#DD0000\"]\"\n </tr>\"[/span][span style=\"color:#007700\"]; echo [/span][span style=\"color:#DD0000\"]\"\n <tr class=\'ncolour2\' height=\'10\'>\"[/span][span style=\"color:#007700\"]; echo [/span][span style=\"color:#DD0000\"]\"\n <td>\"[/span][span style=\"color:#007700\"]; echo [/span][span style=\"color:#0000BB\"]$totalonline [/span][span style=\"color:#007700\"]. [/span][span style=\"color:#DD0000\"]\" Users Online.<br /><br />\"[/span][span style=\"color:#007700\"]; [/span][span style=\"color:#FF8000\"]//show total users [/span][span style=\"color:#007700\"]echo [/span][span style=\"color:#0000BB\"]$totalguests [/span][span style=\"color:#007700\"]. [/span][span style=\"color:#DD0000\"]\" Guests.<br />\"[/span][span style=\"color:#007700\"]; [/span][span style=\"color:#FF8000\"]//show number of guests [/span][span style=\"color:#007700\"]echo [/span][span style=\"color:#0000BB\"]$totalusers [/span][span style=\"color:#007700\"]. [/span][span style=\"color:#DD0000\"]\" Users.<br /><br />\"[/span][span style=\"color:#007700\"]; [/span][span style=\"color:#FF8000\"]//show number of users [/span][span style=\"color:#007700\"]echo [/span][span style=\"color:#0000BB\"]$users[/span][span style=\"color:#007700\"]; echo [/span][span style=\"color:#DD0000\"]\"\n </td>\"[/span][span style=\"color:#007700\"]; echo [/span][span style=\"color:#DD0000\"]\"\n </tr>\"[/span][span style=\"color:#007700\"]; echo [/span][span style=\"color:#DD0000\"]\"\n</table>\"[/span][span style=\"color:#007700\"]; echo [/span][span style=\"color:#DD0000\"]\"\n</center><br /><br />\"[/span][span style=\"color:#007700\"]; [/span][span style=\"color:#0000BB\"]?> [/span] [/span][!--PHP-Foot--][/div][!--PHP-EFoot--] Quote Link to comment https://forums.phpfreaks.com/topic/2673-sql-query-is-failing/ Share on other sites More sharing options...
ryanlwh Posted October 16, 2005 Share Posted October 16, 2005 ip is the primary key. the update query has the condition of OR. so whenever username equals spyke01 (ip doesn't have to match because of OR), ip gets changed. Since it's primary key, you're not allowed to change it to an ip that's already exists. Quote Link to comment https://forums.phpfreaks.com/topic/2673-sql-query-is-failing/#findComment-8872 Share on other sites More sharing options...
spyke01 Posted October 16, 2005 Author Share Posted October 16, 2005 hmm what if i make the timestamp the primary key, will it still fail? Quote Link to comment https://forums.phpfreaks.com/topic/2673-sql-query-is-failing/#findComment-8878 Share on other sites More sharing options...
rsnell Posted October 16, 2005 Share Posted October 16, 2005 You trying to update the key field of the table, drop the key = from the update ( but leave it in the where ) and it should work. Quote Link to comment https://forums.phpfreaks.com/topic/2673-sql-query-is-failing/#findComment-8880 Share on other sites More sharing options...
spyke01 Posted October 16, 2005 Author Share Posted October 16, 2005 well if the key is the ip address, and they log in on another computer, then the ip address wont be correct, wht if i make it so there is no key? Quote Link to comment https://forums.phpfreaks.com/topic/2673-sql-query-is-failing/#findComment-8884 Share on other sites More sharing options...
ryanlwh Posted October 16, 2005 Share Posted October 16, 2005 no key would be ok... timestamp can be key but what if two users login the same second? Quote Link to comment https://forums.phpfreaks.com/topic/2673-sql-query-is-failing/#findComment-8886 Share on other sites More sharing options...
neylitalo Posted October 16, 2005 Share Posted October 16, 2005 wht if i make it so there is no key? I see nothing wrong with that. But why can't you just make the username primary, and then check the username to do the UPDATE? Quote Link to comment https://forums.phpfreaks.com/topic/2673-sql-query-is-failing/#findComment-8887 Share on other sites More sharing options...
spyke01 Posted October 16, 2005 Author Share Posted October 16, 2005 you all have made valuable points, the only problem im having is: if ip is key then it fails if timestamp is key then it fails if two people do it at the same time if there is no key then i end up with several entries of the same username if i use username as the key then the guests im trying to kep track of dont get counted right Quote Link to comment https://forums.phpfreaks.com/topic/2673-sql-query-is-failing/#findComment-8889 Share on other sites More sharing options...
ryanlwh Posted October 17, 2005 Share Posted October 17, 2005 can you assign a random (or incremental) number to a guest when they arrive, and append it to the username? Guest123, Guest124. maybe a separated id field which would be PRIMARY KEY AUTO_INCREMENT, and then change username to UNIQUE, then, using the above method, you can use either one to check for access and updates. Quote Link to comment https://forums.phpfreaks.com/topic/2673-sql-query-is-failing/#findComment-8890 Share on other sites More sharing options...
spyke01 Posted October 17, 2005 Author Share Posted October 17, 2005 so add say an table olumn named id set to autoincriment then say attach the id value to the end of guest and then change i via ip or username? Quote Link to comment https://forums.phpfreaks.com/topic/2673-sql-query-is-failing/#findComment-8892 Share on other sites More sharing options...
spyke01 Posted October 17, 2005 Author Share Posted October 17, 2005 ok ive figured it out, mainly what im doing is deleting out the old entries and then adding them again if the user is still online, later on im going to set it up so that it adds/deletes at the loading of the page, that way i can see whos on a certain page and it be correct, heres the source for anyone who needs it, thank for all your help guys!! ^^ [!--PHP-Head--][div class=\'phptop\']PHP[/div][div class=\'phpmain\'][!--PHP-EHead--] [span style=\"color:#0000BB\"]<?php $timeoutseconds [/span][span style=\"color:#007700\"]= ([/span][span style=\"color:#0000BB\"]$_SESSION[/span][span style=\"color:#007700\"][[/span][span style=\"color:#DD0000\"]\'username\'[/span][span style=\"color:#007700\"]]) ? [/span][span style=\"color:#DD0000\"]\'2592000\' [/span][span style=\"color:#007700\"]: [/span][span style=\"color:#DD0000\"]\'1200\'[/span][span style=\"color:#007700\"]; [/span][span style=\"color:#0000BB\"]$timestamp[/span][span style=\"color:#007700\"]=[/span][span style=\"color:#0000BB\"]time[/span][span style=\"color:#007700\"](); [/span][span style=\"color:#0000BB\"]$timeout[/span][span style=\"color:#007700\"]=[/span][span style=\"color:#0000BB\"]$timestamp[/span][span style=\"color:#007700\"]+[/span][span style=\"color:#0000BB\"]$timeoutseconds[/span][span style=\"color:#007700\"]; [/span][span style=\"color:#0000BB\"]$ip [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#0000BB\"]$REMOTE_ADDR[/span][span style=\"color:#007700\"]; [/span][span style=\"color:#0000BB\"]$currentuser [/span][span style=\"color:#007700\"]= ([/span][span style=\"color:#0000BB\"]$_SESSION[/span][span style=\"color:#007700\"][[/span][span style=\"color:#DD0000\"]\'username\'[/span][span style=\"color:#007700\"]]) ? [/span][span style=\"color:#0000BB\"]$_SESSION[/span][span style=\"color:#007700\"][[/span][span style=\"color:#DD0000\"]\'username\'[/span][span style=\"color:#007700\"]] : [/span][span style=\"color:#DD0000\"]\'Guest\'[/span][span style=\"color:#007700\"]; [/span][span style=\"color:#FF8000\"]//========================================================== // Delete any user entries that match the current user // This should take care of multiple user entries //========================================================== [/span][span style=\"color:#0000BB\"]$sql [/span][span style=\"color:#007700\"]= ([/span][span style=\"color:#0000BB\"]$_SESSION[/span][span style=\"color:#007700\"][[/span][span style=\"color:#DD0000\"]\'username\'[/span][span style=\"color:#007700\"]]) ? [/span][span style=\"color:#DD0000\"]\"DELETE FROM `usersonline` WHERE username = \'\" [/span][span style=\"color:#007700\"]. [/span][span style=\"color:#0000BB\"]$_SESSION[/span][span style=\"color:#007700\"][[/span][span style=\"color:#DD0000\"]\'username\'[/span][span style=\"color:#007700\"]] . [/span][span style=\"color:#DD0000\"]\"\' OR ip = \'$ip\'\" [/span][span style=\"color:#007700\"]: [/span][span style=\"color:#DD0000\"]\"DELETE FROM `usersonline` WHERE ip = \'$ip\'\"[/span][span style=\"color:#007700\"]; [/span][span style=\"color:#0000BB\"]mysql_query[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$sql[/span][span style=\"color:#007700\"]) or die([/span][span style=\"color:#DD0000\"]\'<br />Error, insert query failed\' [/span][span style=\"color:#007700\"]. [/span][span style=\"color:#0000BB\"]$sql[/span][span style=\"color:#007700\"]); [/span][span style=\"color:#FF8000\"]//========================================================== // Add this user to database //========================================================== [/span][span style=\"color:#0000BB\"]$sql [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#DD0000\"]\"INSERT INTO `usersonline` (username, timestamp, ip, file)\"[/span][span style=\"color:#007700\"]. [/span][span style=\"color:#DD0000\"]\"VALUES(\'$currentuser\', \'$timestamp\', \'$ip\', \'\"[/span][span style=\"color:#007700\"].[/span][span style=\"color:#0000BB\"]$_SERVER[/span][span style=\"color:#007700\"][[/span][span style=\"color:#DD0000\"]\'PHP_SELF\'[/span][span style=\"color:#007700\"]].[/span][span style=\"color:#DD0000\"]\"\')\"[/span][span style=\"color:#007700\"]; [/span][span style=\"color:#0000BB\"]mysql_query[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$sql[/span][span style=\"color:#007700\"]) or die([/span][span style=\"color:#DD0000\"]\'<br />Error, insert query failed\' [/span][span style=\"color:#007700\"]. [/span][span style=\"color:#0000BB\"]$sql[/span][span style=\"color:#007700\"]); [/span][span style=\"color:#FF8000\"]//========================================================== // Delete users that have been online for more then \"$timeoutseconds\" seconds //========================================================== [/span][span style=\"color:#007700\"]@[/span][span style=\"color:#0000BB\"]mysql_query[/span][span style=\"color:#007700\"]([/span][span style=\"color:#DD0000\"]\"DELETE FROM `usersonline` WHERE timestamp>$timeout\"[/span][span style=\"color:#007700\"]); [/span][span style=\"color:#FF8000\"]//========================================================== // Shows total users online as well as username //========================================================== [/span][span style=\"color:#0000BB\"]$sql [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#DD0000\"]\"SELECT * FROM `usersonline`\"[/span][span style=\"color:#007700\"]; [/span][span style=\"color:#0000BB\"]$result [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#0000BB\"]mysql_query[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$sql[/span][span style=\"color:#007700\"]) or die ([/span][span style=\"color:#0000BB\"]mysql_error[/span][span style=\"color:#007700\"]().[/span][span style=\"color:#DD0000\"]\"<br />Couldn\'t execute query: $sql\"[/span][span style=\"color:#007700\"]); if([/span][span style=\"color:#0000BB\"]mysql_num_rows[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$result[/span][span style=\"color:#007700\"]) == [/span][span style=\"color:#0000BB\"]0[/span][span style=\"color:#007700\"]) [/span][span style=\"color:#FF8000\"]//if NO results, stop the script & return the error message [/span][span style=\"color:#007700\"]{ echo [/span][span style=\"color:#DD0000\"]\"\nError reading usersonline table section 2.<br /><br />\"[/span][span style=\"color:#007700\"]; } else [/span][span style=\"color:#FF8000\"]//if result found, run the rest of the script [/span][span style=\"color:#007700\"]{ [/span][span style=\"color:#0000BB\"]$totalonline [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#0000BB\"]mysql_num_rows[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$result[/span][span style=\"color:#007700\"]); } [/span][span style=\"color:#0000BB\"]mysql_free_result[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$result[/span][span style=\"color:#007700\"]); [/span][span style=\"color:#FF8000\"]//free our query //========================================================== // Find out whos online and if they\'re a user make a link // to their profile page //========================================================== [/span][span style=\"color:#0000BB\"]$sql [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#DD0000\"]\"SELECT * FROM `usersonline` WHERE username != \'Guest\'\"[/span][span style=\"color:#007700\"]; [/span][span style=\"color:#0000BB\"]$result [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#0000BB\"]mysql_query[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$sql[/span][span style=\"color:#007700\"]) or die ([/span][span style=\"color:#0000BB\"]mysql_error[/span][span style=\"color:#007700\"]().[/span][span style=\"color:#DD0000\"]\"<br />Couldn\'t execute query: $sql\"[/span][span style=\"color:#007700\"]); [/span][span style=\"color:#0000BB\"]$totalguests [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#0000BB\"]$totalonline [/span][span style=\"color:#007700\"]- [/span][span style=\"color:#0000BB\"]mysql_num_rows[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$result[/span][span style=\"color:#007700\"]); [/span][span style=\"color:#0000BB\"]$totalusers [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#0000BB\"]$totalonline [/span][span style=\"color:#007700\"]- [/span][span style=\"color:#0000BB\"]$totalguests[/span][span style=\"color:#007700\"]; if([/span][span style=\"color:#0000BB\"]mysql_num_rows[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$result[/span][span style=\"color:#007700\"]) == [/span][span style=\"color:#0000BB\"]0[/span][span style=\"color:#007700\"]) [/span][span style=\"color:#FF8000\"]//if NO results, stop the script & return the error message [/span][span style=\"color:#007700\"]{ [/span][span style=\"color:#0000BB\"]$users [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#DD0000\"]\"No registered users logged in.\"[/span][span style=\"color:#007700\"]; [/span][span style=\"color:#FF8000\"]//add username to our list ^^ [/span][span style=\"color:#007700\"]} else [/span][span style=\"color:#FF8000\"]//if result found, run the rest of the script [/span][span style=\"color:#007700\"]{ while ( [/span][span style=\"color:#0000BB\"]$row [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#0000BB\"]mysql_fetch_array[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$result[/span][span style=\"color:#007700\"]) ) { [/span][span style=\"color:#0000BB\"]$sql2 [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#DD0000\"]\"SELECT * FROM `users` WHERE username = \'$row[username]\'\"[/span][span style=\"color:#007700\"]; [/span][span style=\"color:#0000BB\"]$result2 [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#0000BB\"]mysql_query[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$sql2[/span][span style=\"color:#007700\"]) or die ([/span][span style=\"color:#0000BB\"]mysql_error[/span][span style=\"color:#007700\"]().[/span][span style=\"color:#DD0000\"]\"<br />Couldn\'t execute query: $sql\"[/span][span style=\"color:#007700\"]); if([/span][span style=\"color:#0000BB\"]mysql_num_rows[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$result2[/span][span style=\"color:#007700\"]) == [/span][span style=\"color:#0000BB\"]0[/span][span style=\"color:#007700\"]) [/span][span style=\"color:#FF8000\"]//if NO results, stop the script & return the error message [/span][span style=\"color:#007700\"]{ [/span][span style=\"color:#0000BB\"]$users [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#DD0000\"]\"No registered users logged in.\"[/span][span style=\"color:#007700\"]; [/span][span style=\"color:#FF8000\"]//add username to our list ^^ [/span][span style=\"color:#007700\"]} else [/span][span style=\"color:#FF8000\"]//if result found, run the rest of the script [/span][span style=\"color:#007700\"]{ while ( [/span][span style=\"color:#0000BB\"]$row2 [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#0000BB\"]mysql_fetch_array[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$result2[/span][span style=\"color:#007700\"]) ) { [/span][span style=\"color:#0000BB\"]$users [/span][span style=\"color:#007700\"].= [/span][span style=\"color:#DD0000\"]\"<a href=\'$menuvar[PROFILE]&action=viewprofile&id=\" [/span][span style=\"color:#007700\"]. [/span][span style=\"color:#0000BB\"]$row2[/span][span style=\"color:#007700\"][[/span][span style=\"color:#DD0000\"]\'userid\'[/span][span style=\"color:#007700\"]] . [/span][span style=\"color:#DD0000\"]\"\'>$row2[username]</a> \"[/span][span style=\"color:#007700\"]; } } [/span][span style=\"color:#0000BB\"]mysql_free_result[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$result2[/span][span style=\"color:#007700\"]); [/span][span style=\"color:#FF8000\"]//free our query [/span][span style=\"color:#007700\"]} } [/span][span style=\"color:#0000BB\"]mysql_free_result[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$result[/span][span style=\"color:#007700\"]); [/span][span style=\"color:#FF8000\"]//free our query //========================================================== //Print out our nice table //========================================================== [/span][span style=\"color:#007700\"]echo [/span][span style=\"color:#DD0000\"]\"\n<center>\"[/span][span style=\"color:#007700\"]; echo [/span][span style=\"color:#DD0000\"]\"\n<table class=\'forumborder\' border=\'0\' cellpadding=\'0\' cellspacing=\'1\' width=\'600\'>\"[/span][span style=\"color:#007700\"]; echo [/span][span style=\"color:#DD0000\"]\"\n <tr class=\'title2\' nowrap height=\'10\'>\"[/span][span style=\"color:#007700\"]; echo [/span][span style=\"color:#DD0000\"]\"\n <td><center><b>Online Stats</b></center></td>\"[/span][span style=\"color:#007700\"]; echo [/span][span style=\"color:#DD0000\"]\"\n </tr>\"[/span][span style=\"color:#007700\"]; echo [/span][span style=\"color:#DD0000\"]\"\n <tr class=\'ncolour2\' height=\'10\'>\"[/span][span style=\"color:#007700\"]; echo [/span][span style=\"color:#DD0000\"]\"\n <td>\"[/span][span style=\"color:#007700\"]; echo [/span][span style=\"color:#0000BB\"]$totalonline [/span][span style=\"color:#007700\"]. [/span][span style=\"color:#DD0000\"]\" Users Online.<br /><br />\"[/span][span style=\"color:#007700\"]; [/span][span style=\"color:#FF8000\"]//show total users [/span][span style=\"color:#007700\"]echo [/span][span style=\"color:#0000BB\"]$totalguests [/span][span style=\"color:#007700\"]. [/span][span style=\"color:#DD0000\"]\" Guests.<br />\"[/span][span style=\"color:#007700\"]; [/span][span style=\"color:#FF8000\"]//show number of guests [/span][span style=\"color:#007700\"]echo [/span][span style=\"color:#0000BB\"]$totalusers [/span][span style=\"color:#007700\"]. [/span][span style=\"color:#DD0000\"]\" Users.<br /><br />\"[/span][span style=\"color:#007700\"]; [/span][span style=\"color:#FF8000\"]//show number of users [/span][span style=\"color:#007700\"]echo [/span][span style=\"color:#0000BB\"]$users[/span][span style=\"color:#007700\"]; echo [/span][span style=\"color:#DD0000\"]\"\n </td>\"[/span][span style=\"color:#007700\"]; echo [/span][span style=\"color:#DD0000\"]\"\n </tr>\"[/span][span style=\"color:#007700\"]; echo [/span][span style=\"color:#DD0000\"]\"\n</table>\"[/span][span style=\"color:#007700\"]; echo [/span][span style=\"color:#DD0000\"]\"\n</center><br /><br />\"[/span][span style=\"color:#007700\"]; [/span][span style=\"color:#0000BB\"]?> [/span] [/span][!--PHP-Foot--][/div][!--PHP-EFoot--] Quote Link to comment https://forums.phpfreaks.com/topic/2673-sql-query-is-failing/#findComment-8916 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.