Jump to content

Checking a field


Ph0enix

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
Link to comment
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
Link to comment
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
Link to comment
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
Link to comment
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]
Link to comment
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
Link to comment
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
Link to comment
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]
Link to comment
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.
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.