Jump to content


Photo

Checking a field


  • Please log in to reply
13 replies to this topic

#1 Ph0enix

Ph0enix
  • Members
  • PipPipPip
  • Advanced Member
  • 64 posts

Posted 30 July 2006 - 03:17 AM

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.

<?php
session_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>";
}
}
?>

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?
Thanks
Max
[url=http://www.slimphoenix.com][/url]

#2 Drumminxx

Drumminxx
  • Members
  • PipPipPip
  • Advanced Member
  • 92 posts
  • LocationUSA

Posted 30 July 2006 - 03:21 AM

why not redirect them to the login page

if (empty($_SESSION['username'])) {
header ('Location: login.php');
}else{
...
:: gucci.com ::
:: blockbuster.com ::

--------------------

#3 Ph0enix

Ph0enix
  • Members
  • PipPipPip
  • Advanced Member
  • 64 posts

Posted 30 July 2006 - 03:25 AM

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.
Thanks
Max
[url=http://www.slimphoenix.com][/url]

#4 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 30 July 2006 - 03:35 AM

try
$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{

Hope this helps,
Tom

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#5 Ph0enix

Ph0enix
  • Members
  • PipPipPip
  • Advanced Member
  • 64 posts

Posted 30 July 2006 - 03:43 AM

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?
Thanks
Max
[url=http://www.slimphoenix.com][/url]

#6 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 30 July 2006 - 03:48 AM

Yes try this.

<?php
session_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-->

Hope this one will work

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#7 Ph0enix

Ph0enix
  • Members
  • PipPipPip
  • Advanced Member
  • 64 posts

Posted 30 July 2006 - 03:53 AM

Hi, thanks tomfmason.
Nope, still doesnt work. Grrr!
This is soooo annoying.
Is there anything else i can try?
Thanks
Max


[url=http://www.slimphoenix.com][/url]

#8 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 30 July 2006 - 04:09 AM

ok try this

<?php
session_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-->

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#9 Ph0enix

Ph0enix
  • Members
  • PipPipPip
  • Advanced Member
  • 64 posts

Posted 30 July 2006 - 04:14 AM

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)
Thanks
Max
[url=http://www.slimphoenix.com][/url]

#10 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 30 July 2006 - 04:26 AM

ok are you getting any errors.

I tried this on my site and it echoed sucess.

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";
}		
?>



I would recomend trying the code posted below and see if it works. Name this test.php or something

<?php
include("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";
?>

After you do this with a username that you know is assiocated with a clan try it with one that is not

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#11 corbin

corbin
  • Staff Alumni
  • Advanced Member
  • 8,129 posts

Posted 30 July 2006 - 04:37 AM

try
<?php
include("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";
?>

Why doesn't anyone ever say hi, hey, or whad up world?

#12 Ph0enix

Ph0enix
  • Members
  • PipPipPip
  • Advanced Member
  • 64 posts

Posted 30 July 2006 - 12:45 PM

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
[url=http://www.slimphoenix.com][/url]

#13 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 30 July 2006 - 12:54 PM

I don't know why I used mysql_num_rows instead of mysql_result but you are welcome.

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#14 Balmung-San

Balmung-San
  • Members
  • PipPipPip
  • Advanced Member
  • 327 posts

Posted 30 July 2006 - 01:46 PM

I don't know why I used mysql_num_rows instead of mysql_result but you are welcome.

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
($result['username'] != "")
you would need
($result['clanname'] != "")
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.
"Paranoia is very useful in this work. ...If your cryptographic system can survive the paranoia model, it has at least a fighting chance of surviving in the real world." - Niels Ferguson & Bruce Schneier

They say there's more then one way to skin a cat.
In programming, there's no wrong way to skin a cat, just more efficent ways of doing so.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users