Jump to content

Archived

This topic is now archived and is closed to further replies.

Ph0enix

Checking a field

Recommended Posts

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]<?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>";
}
}
?>
[/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?
Thanks
Max

Share this post


Link to post
Share on other sites
why not redirect them to the login page

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Yes try this.

[code=php:0]<?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-->
[/code]

Hope this one will work

Share this post


Link to post
Share on other sites
Hi, thanks tomfmason.
Nope, still doesnt work. Grrr!
This is soooo annoying.
Is there anything else i can try?
Thanks
Max

Share this post


Link to post
Share on other sites
ok try this

[code=php:0]<?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-->
[/code]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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]
<?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";
?>[/code]

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

Share this post


Link to post
Share on other sites
try
[code]
<?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";
?>
[/code]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
I don't know why I used mysql_num_rows instead of mysql_result but you are welcome.

Share this post


Link to post
Share on other sites
[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.

Share this post


Link to post
Share on other sites

×

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.