Ph0enix Posted July 30, 2006 Share Posted July 30, 2006 Hi, im creating a league script and i have come across a problem. On the page where people are able to register clans, i want to check to see if they are allready in a clan. This is my script.[code]<?phpsession_start();include "connect.php";if (empty($_SESSION['username'])) { echo "You must be logged in to access this page. <a href='login.php'>Click here</a> to login.";}else{$username = $_SESSION['username'];$result = mysql_fetch_array(mysql_query("SELECT clanname FROM users WHERE username = '$username'"));if (($result['username'] != "")) { echo "You are allready in a clan.";}else{ echo"<form action='clanck.php' method='POST'><table align='center' border='0' cellspacing='0'><tr><td>Clan Tag:</td><td><input type='text' name='clantag'></td></tr><tr><td>Clan Name:</td><td><input type='text' name='clanname'></tr><tr><td>Clan Site:</td><td><input type='text' name='clansite'></td></tr><tr><td><input type='submit' value='Register Clan'></td></tr></table></form>";}}?>[/code]The first bit works, so if they are not logged in then they cant access the page. But the next bit doesnt. I dont get any error, but the form is always shown even if they are in a clan. Can someone help me please?ThanksMax Quote Link to comment https://forums.phpfreaks.com/topic/15987-checking-a-field/ Share on other sites More sharing options...
Drumminxx Posted July 30, 2006 Share Posted July 30, 2006 why not redirect them to the login pageif (empty($_SESSION['username'])) { header ('Location: login.php');}else{... Quote Link to comment https://forums.phpfreaks.com/topic/15987-checking-a-field/#findComment-65701 Share on other sites More sharing options...
Ph0enix Posted July 30, 2006 Author Share Posted July 30, 2006 Hi, thanks for your fast reply.The first part of the script works fine its just the next bit..$username = $_SESSION['username'];$result = mysql_fetch_array(mysql_query("SELECT clanname FROM users WHERE username = '$username'"));if (($result['username'] != "")) { echo "You are allready in a clan.";even if the user is in a clan the form still shows.But if they are in a clan i want a message to come up saying "you are allready in a clan" and then thats it.ThanksMax Quote Link to comment https://forums.phpfreaks.com/topic/15987-checking-a-field/#findComment-65702 Share on other sites More sharing options...
tomfmason Posted July 30, 2006 Share Posted July 30, 2006 try [code=php:0]$sql = mysql_query("SELECT clanname FROM users WHERE username = '$username'");$result = mysql_num_rows($sql);if ($result > 0) { echo "you are already a member of a clan"; include("some_page.php"); exit;}else{[/code]Hope this helps,Tom Quote Link to comment https://forums.phpfreaks.com/topic/15987-checking-a-field/#findComment-65704 Share on other sites More sharing options...
Ph0enix Posted July 30, 2006 Author Share Posted July 30, 2006 Hi, thanks tomfmason but it still doesn't work :(Now if the user is in a clan or not, the output is always "You are allready in a clan".Any other suggestions?ThanksMax Quote Link to comment https://forums.phpfreaks.com/topic/15987-checking-a-field/#findComment-65706 Share on other sites More sharing options...
tomfmason Posted July 30, 2006 Share Posted July 30, 2006 Yes try this.[code=php:0]<?phpsession_start();include "connect.php";if (!$_SESSION['username']) { echo "You must be logged in to access this page. <a href='login.php'>Click here</a> to login.";}else{ $username = $_SESSION['username']; $sql = mysql_query("SELECT clanname FROM users WHERE username = '$username'"); $result = mysql_num_rows($sql); if ($result > 0) { echo "you are already a member of a clan"; include("some_page.php"); exit; }}?><!---Put your html below here-->[/code]Hope this one will work Quote Link to comment https://forums.phpfreaks.com/topic/15987-checking-a-field/#findComment-65708 Share on other sites More sharing options...
Ph0enix Posted July 30, 2006 Author Share Posted July 30, 2006 Hi, thanks tomfmason.Nope, still doesnt work. Grrr!This is soooo annoying.Is there anything else i can try?ThanksMax Quote Link to comment https://forums.phpfreaks.com/topic/15987-checking-a-field/#findComment-65710 Share on other sites More sharing options...
tomfmason Posted July 30, 2006 Share Posted July 30, 2006 ok try this [code=php:0]<?phpsession_start();include("connect.php");if (!$_SESSION['username']) { echo "You must be logged in to access this page."; include("Yourloginform.php"); exit;}$username = $_SESSION['username'];$sql = mysql_query("SELECT clanname FROM users WHERE username = '$username'");$result = mysql_num_rows($sql);if ($result > 0) { echo "you are already a member of a clan"; include("some_page.php"); exit;}?><!---Put your html below here-->[/code] Quote Link to comment https://forums.phpfreaks.com/topic/15987-checking-a-field/#findComment-65716 Share on other sites More sharing options...
Ph0enix Posted July 30, 2006 Author Share Posted July 30, 2006 Argh!! Still doesn't work :(I was thinking, should i set the default for the column clanname to 1 and then check the field to see if it is equal to 1, and if it is then they are not in a clan.If i should try this how would i do it? :-[(thanks for all the help your giving me tomfmason)ThanksMax Quote Link to comment https://forums.phpfreaks.com/topic/15987-checking-a-field/#findComment-65718 Share on other sites More sharing options...
tomfmason Posted July 30, 2006 Share Posted July 30, 2006 ok are you getting any errors.I tried this on my site and it echoed sucess.[code=php:0]include("db.php");$email ="test@test.com";$sql = mysql_query("SELECT test1 FROM test WHERE email = '$email'");$result = mysql_num_rows($sql);if ($result > 0) { echo "sucess";}else{ echo "This test did not work";} ?>[/code]I would recomend trying the code posted below and see if it works. Name this test.php or something[code=php:0]<?phpinclude("connect.php");//pick a username that you know is a member of a clan$username = "";$sql = mysql_query("SELECT clanname FROM users WHERE username = '$username'") or die(mysql_error());$result = mysql_num_rows($sql);if ($result > 0) { echo "This username is associated with a clan";}else{ echo "This username is not associated with a clan";?>[/code]After you do this with a username that you know is assiocated with a clan try it with one that is not Quote Link to comment https://forums.phpfreaks.com/topic/15987-checking-a-field/#findComment-65721 Share on other sites More sharing options...
corbin Posted July 30, 2006 Share Posted July 30, 2006 try [code]<?phpinclude("connect.php");//pick a username that you know is a member of a clan$username = "";$sql = mysql_query("SELECT clanname FROM users WHERE username = '$username'") or die(mysql_error());$result = mysql_result($sql,0,'clanname');if (($result != "") || ($result != null) || (!isset($result))) { echo "This username is associated with a clan";}else{ echo "This username is not associated with a clan";?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/15987-checking-a-field/#findComment-65723 Share on other sites More sharing options...
Ph0enix Posted July 30, 2006 Author Share Posted July 30, 2006 I tried them both and the first one says "That you are associated with a clan" whether the field is empty or not. The second one works.. YES!!! Finally.Thank you both for helping me. I really appreciate it. :D Quote Link to comment https://forums.phpfreaks.com/topic/15987-checking-a-field/#findComment-65871 Share on other sites More sharing options...
tomfmason Posted July 30, 2006 Share Posted July 30, 2006 I don't know why I used mysql_num_rows instead of mysql_result but you are welcome. Quote Link to comment https://forums.phpfreaks.com/topic/15987-checking-a-field/#findComment-65873 Share on other sites More sharing options...
Balmung-San Posted July 30, 2006 Share Posted July 30, 2006 [quote author=tomfmason link=topic=102288.msg405918#msg405918 date=1154264064]I don't know why I used mysql_num_rows instead of mysql_result but you are welcome.[/quote]I think that has to do with the way the query was formatted. As it looks to me there is a users database with a clanname associated with the user. As it looks, if the user exists then it will return a result of 1. With the way the query is formatted you have to check the output as opposed to the number of rows.The original code would have worked, except that instead of[code]($result['username'] != "")[/code]you would need[code]($result['clanname'] != "")[/code]as username isn't returned by your query, and it wouldn't make sense to check the username if you wanted to format it differently based on wether or not they're in a clan. Quote Link to comment https://forums.phpfreaks.com/topic/15987-checking-a-field/#findComment-65887 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.