Jump to content

Check userID is not in clanID


karimali831

Recommended Posts

Hi,

 

Can someone tell me the elseif code that disallows a user joining a team if their userID is already in clanID.

Hope this makes sense. Take a look at the attatchment, it shows when user (ID -> 325) joins a team (ID -> 187), it allows it multiple of times.  (two rows queried)

 

[attachment deleted by admin]

Link to comment
Share on other sites

Okay, I'm not sure where to put this. Here is the code:

 

	$result2 = safe_query("SELECT * FROM ".PREFIX."cup_clan_members WHERE userID = '$userID' && clanID = '$clanID'");
	if(mysql_num_rows($result2)==3)
	echo '<center><b>You already are in 3 teams!</b><br>Please leave a team first.</center>';

==================
ADD HERE?
==================

else{
	$result = safe_query("SELECT * FROM ".PREFIX."cup_all_clans ORDER BY name ASC");		
	$clan .= '<option value="0" selected="selected"> - Please choose a Team - </option>';
	while($ds=mysql_fetch_array($result)) {
		$clan .= '<option value="'.$ds[iD].'">'.$ds[name].'</option>';
	}

 

Thanks for help.

Link to comment
Share on other sites

I think it's good idea to set unique index on user_id in this table since player can be joined in one clan, but you shouldn't (if I understand table structure correctly)set uniq ID on clan_id since one clan has several players so there gona be more then one row with same clan_id, or I'm totaly wrong?

Link to comment
Share on other sites

I think it's good idea to set unique index on user_id in this table since player can be joined in one clan, but you shouldn't (if I understand table structure correctly)set uniq ID on clan_id since one clan has several players so there gona be more then one row with same clan_id, or I'm totaly wrong?

 

That's why I advise setting up an index on TWO columns instead of one.

 

ALTER TABLE tableName ADD UNIQUE clan_id_user_id (clan_id,user_id)

Link to comment
Share on other sites

Ok, I set unique for userID and clanID now you get this error:

 

# Query failed: errorno=1062

# error=Duplicate entry '187-325' for key 2

# query=INSERT INTO ficocup_cup_clan_members (clanID, userID, function) VALUES ('187', '325', 'Member')

 

which is pretty good, but to those that don't understand this error can't the error be changed?

Link to comment
Share on other sites

Yes it can. Where you do your insert query

 

$sql = "INSERT INTO ficocup_cup_clan_members (clanID, userID, function) VALUES ('187', '325', 'Member')";
if(!$result = mysql_query($sql)) {
  if(mysql_errno() == 1062) {
     echo "You're already a member of this clan";
  }
}

Link to comment
Share on other sites

Ok, now I got :

 

$ergebnis2 = safe_query("SELECT * FROM ".PREFIX."cup_clan_members WHERE userID = '$userID' && clanID = '$clanID'");
	if(mysql_num_rows($ergebnis2)==3)
	echo '<center><b>You already are in 3 teams!</b><br>Please leave a team first.</center>';

                elseif(mysql_errno() == 1062) {
                echo "You're already a member of this clan";
}

else{

 

but still showing sql error and not echo?

Link to comment
Share on other sites

1. This is SELECTing part (which you no longer need). You need to use this code when INSERT is done.

 

2. Your safe_query function has some error catching in it. It's not standard php function, so I can't tell you much about it. Using mysql_query instead (in this one place) you should be fine.

Link to comment
Share on other sites

  • 2 weeks later...

I haven't seen this function, so I can't tell you if it is indeed safer. It comes with your CMS and is not part of PHP.

Even if it is safer, it seems it has it's own error catching logic built in, which will interfere with what we're trying to do here, so this one time you're forced to use mysql_query() (if you want to use the method presented here). As long as you take care to properly escape all values in the query, there is no additional risk.

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.