conan318 Posted April 18, 2011 Share Posted April 18, 2011 im trying to insert $myusername into a table only if $myusername does not exist is best to call the insert ignore or is there better way of doing this? also trying to make a list of logged in users which is allways updating when the page is auto refreshed whats the best to go about checking if the users is still there. Quote Link to comment https://forums.phpfreaks.com/topic/234052-insert-ignore/ Share on other sites More sharing options...
Muddy_Funster Posted April 18, 2011 Share Posted April 18, 2011 INSERT IGNORE does not do what you want. What you need to do is query the table first to see if the name exists, then run the insert if it does not. INSERT IGNORE just suppresses error breaks during the query. To answer your second question, you will need to let us know what method you have of recording which users are logged in and which are not. Quote Link to comment https://forums.phpfreaks.com/topic/234052-insert-ignore/#findComment-1202969 Share on other sites More sharing options...
conan318 Posted April 18, 2011 Author Share Posted April 18, 2011 do you mean something like this mysql_query("IF NOT EXISTS(SELECT * FROM logedin WHERE username=$myusername) THEN INSERT INTO logedin (username) VALUES ('$myusername')"); pretty much im using if(!$_SESSION['myusername']){ //if usernames don't match redirect to login page header("location: main_login.php"); } $myusername=$_SESSION['myusername']; then inserting my $myusername into the logedin table. the page refreshs every 60 seconds and is adding the username into the table again. but i need to somehow check if all users are still there when then page refreshers and remove the ones who arent there. thats the goal thanks Quote Link to comment https://forums.phpfreaks.com/topic/234052-insert-ignore/#findComment-1202984 Share on other sites More sharing options...
Muddy_Funster Posted April 18, 2011 Share Posted April 18, 2011 not exactly. you would need to use two queries: $qry1 = "SELECT username FROM loggedin WHERE username = '".$myusername"'"; $check = mysql_query($qry1); if (mysql_num_rows($check) < 1){ $qry2 = "INSERT INTO loggedin SET username = '".$myusername."'"; mysql_query($qry2) } something like that should do, not tested it though. To show the list of logged in users you would use another SELECT query, that would be run each time the page loads: $qlist = "SELECT DISTINCT username FROM logedin"; $rlist = mysql_query($qlist); while ($row = mysql_fetch_assoc($rlist)){ echo $row['username'].' | '; } again, untested. This is just the bare minimum of what you would need, there is no error checking or catching here so you should look to expand on this. Quote Link to comment https://forums.phpfreaks.com/topic/234052-insert-ignore/#findComment-1202997 Share on other sites More sharing options...
conan318 Posted April 18, 2011 Author Share Posted April 18, 2011 thanks mate will give that a go and let you know how it went Quote Link to comment https://forums.phpfreaks.com/topic/234052-insert-ignore/#findComment-1202999 Share on other sites More sharing options...
conan318 Posted April 18, 2011 Author Share Posted April 18, 2011 just tested it, and it works the first time a users enters the room and writes to database but after page refreshers the list disappears if i logged out and in nothing. deleted entrys from the database and re logged in worked intill the page refreshes.. Quote Link to comment https://forums.phpfreaks.com/topic/234052-insert-ignore/#findComment-1203020 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.