karimali831 Posted February 20, 2010 Share Posted February 20, 2010 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] Quote Link to comment Share on other sites More sharing options...
sader Posted February 20, 2010 Share Posted February 20, 2010 $qr = mysql_query("SELECT id FROM clans WHERE clan_id=xxx AND user_id=yyy LIMIT 1"); if(mysql_num_rows($qr) === 0) { //insert user into clans table } Quote Link to comment Share on other sites More sharing options...
Mchl Posted February 20, 2010 Share Posted February 20, 2010 Create UNIQUE index on (clan_id,user_id) columne. Quote Link to comment Share on other sites More sharing options...
karimali831 Posted February 20, 2010 Author Share Posted February 20, 2010 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. Quote Link to comment Share on other sites More sharing options...
karimali831 Posted February 20, 2010 Author Share Posted February 20, 2010 have I confused anyone? :'( Quote Link to comment Share on other sites More sharing options...
jorley Posted February 20, 2010 Share Posted February 20, 2010 Are you trying to stop them from joining a clan if they are already a member of 3 clans and if they are a current member of the one show? Quote Link to comment Share on other sites More sharing options...
karimali831 Posted February 20, 2010 Author Share Posted February 20, 2010 Yes exackly. The code works if you try and join more than 3 clans, you get error but problem is you can join| the same clan 3 times as you can see from the attatchment. Please say you understand. Quote Link to comment Share on other sites More sharing options...
jorley Posted February 20, 2010 Share Posted February 20, 2010 I think I need a little more info from you. Are there separate tables for each clan? separate member tables for each clan? Are the member ids grouped in a comma list or are they in separate rows? Quote Link to comment Share on other sites More sharing options...
Mchl Posted February 20, 2010 Share Posted February 20, 2010 Create UNIQUE index on (clan_id,user_id) columns. As simple as this. Whenever INSERT query fails with mysql_errno returning 1586, you know user tried to add himself to a clan more than once. Quote Link to comment Share on other sites More sharing options...
karimali831 Posted February 20, 2010 Author Share Posted February 20, 2010 Create UNIQUE index on (clan_id,user_id) columns. As simple as this. Whenever INSERT query fails with mysql_errno returning 1586, you know user tried to add himself to a clan more than once. Ok. Sorry about this but I have never done unique indexing. Perhaps you could tell me how? Quote Link to comment Share on other sites More sharing options...
sader Posted February 20, 2010 Share Posted February 20, 2010 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? Quote Link to comment Share on other sites More sharing options...
Mchl Posted February 20, 2010 Share Posted February 20, 2010 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) Quote Link to comment Share on other sites More sharing options...
karimali831 Posted February 20, 2010 Author Share Posted February 20, 2010 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? Quote Link to comment Share on other sites More sharing options...
Mchl Posted February 20, 2010 Share Posted February 20, 2010 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"; } } Quote Link to comment Share on other sites More sharing options...
karimali831 Posted February 20, 2010 Author Share Posted February 20, 2010 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? Quote Link to comment Share on other sites More sharing options...
Mchl Posted February 20, 2010 Share Posted February 20, 2010 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. Quote Link to comment Share on other sites More sharing options...
karimali831 Posted February 28, 2010 Author Share Posted February 28, 2010 What's the difference? My CMS developer says it's better to use safe_query but I don't know the difference. Quote Link to comment Share on other sites More sharing options...
Mchl Posted March 1, 2010 Share Posted March 1, 2010 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. Quote Link to comment 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.