Jump to content

INSERT IGNORE


conan318

Recommended Posts

 

 

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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..

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.