Jump to content

Problem with IF statment


henrikb

Recommended Posts

Hi friends,

 

Newbie question, if in the wrong place pleasemove the thread..

 

Please see code below, the highlighted code is what is not working. when I call $tb_stat I get "ACTIVE FREE Member" even if cStatus is "p".

 

I am missing something here, please help!

 

$con = db_connect();

 

if ($_GET["userid"]!="" && ($_GET["userid"]!=$_SESSION["nexus_userid"])){

$sql = "UPDATE nexus_users SET nViews=nViews+1 WHERE nUserId=".$_GET["userid"];

$result = mysql_query($sql,$con);

}

 

 

if ($nUserId->cStatus==p) {$tb_stat="Active PRO Member";}

else $tb_stat="ACTIVE FREE Member";

 

Thanks in advance,

 

Henrik

 

Link to comment
Share on other sites

Hi again,

 

Not working, I tried:

 

if ($nUserId->cStatus == ""){

$tb_stat="Active PRO Member";

}else{

$tb_stat="ACTIVE FREE Member";

}

 

echo $tb_stat;

 

leaving the "" emply and then it comes with "Active PRO Member" so it looks like it is not getting cStatus value from the database at all, maybe its because

 

$sql = "SELECT * FROM nexus_users WHERE nUserId=".$_GET["userid"];

$result = mysql_query($sql,$con);

 

is not getting the required values...

 

Thanks again!

 

Henrik

 

Link to comment
Share on other sites

Hi,

 

Is this correct:

 

$con = db_connect();

$sql = "SELECT * FROM nexus_users WHERE $userid=".$_GET["userid"];

$result = mysql_query($sql,$con);

 

 

if ($userid->cStatus == "p"){

$tb_stat="Active PRO Member";

}else{

$tb_stat="ACTIVE FREE Member";

}

 

echo $tb_stat;

 

This is is producing "ACTIVE FREE Member"

 

Thanks,

 

Henrik

 

PS. Checked the database: where nUserId=1 cStatus is "p"

 

 

Link to comment
Share on other sites

$userid = $_GET['id'];
$con = db_connect();
$sql = "SELECT * FROM nexus_users WHERE userid = '$userid';
$result = mysql_query($sql,$con);
$row = mysql_fetch_array($result);
$cStatus = $row['cStatus'];

if ($cStatus == "p"){
$tb_stat="Active PRO Member";
}else{
$tb_stat="ACTIVE FREE Member";
}

echo $tb_stat;

 

This is how I would do it providing you are not posting the information to this page. If the userid gets posted to here from a form then it's a different story. This is also assuming that cStatus is in the db.

Link to comment
Share on other sites

Hi,

 

This is the page members are coming to when they successfully have logged in http://www.xxxxx.com/xxxx.php?userid=1

 

The following code is getting the user ID and updating last time the user logged in etc.

 

$con = db_connect();

 

if ($_GET["userid"]!="" && ($_GET["userid"]!=$_SESSION["nexus_userid"])){

$sql = "UPDATE nexus_users SET nViews=nViews+1 WHERE nUserId=".$_GET["userid"];

$result = mysql_query($sql,$con);

}

 

$sql = "SELECT * FROM nexus_users WHERE nUserId=".$_GET["userid"];

$result = mysql_query($sql,$con);

 

What I am trying to do is have links on this page being active or inactive depending on if the member is a free member or paid member, using the "p" or "f" in the cStatus field in the database to determine what links are active/inactive...

 

Sorry if this explanation is of little value but I hope it makes it easier to understand what I try to accomplish.

 

Thanks in advance,

 

Henrik

 

 

Link to comment
Share on other sites

You could do this with php but considering all the links on that page you might be better off trying to get a javascript that makes all links active or inactive depending on user status. Why not just send paid members to a different page. If user id matches database include("page2.php") or if userid doesn't match db include("page3.php"); which you could just copy and paste the other page and disable the links. Would probably be faster. Plus you could add another statement as to why they should join.

Link to comment
Share on other sites

You need to put some debugging and error checking code into your script.

 

<?php
$con = db_connect();

if ($_GET["userid"]!="" && ($_GET["userid"]!=$_SESSION["nexus_userid"])){
   $sql = "UPDATE nexus_users SET nViews=nViews+1 WHERE nUserId=".$_GET["userid"];
   $result = mysql_query($sql) or die("Problem with Update Query:<pre>$sql</pre><br>" . mysql_error());
}

$sql = "SELECT * FROM nexus_users WHERE nUserId=".$_GET["userid"];
$result = mysql_query($sql) or die("Problem with Select Query:<pre>$sql</pre><br>" . mysql_error());
$row = mysql_fetch_assoc($result);
echo '<pre>' . print_r($row,true) . '</pre>'; // debugging line
?>

 

Also, remember to put all of your code between


tags when posting it.

 

Ken

 

Link to comment
Share on other sites

Ken,

 

The first part that is working fine, the second part is the original code

which I did myself which is not working..

 

Thanks,

 

Henrik

 

 

$con = db_connect();

if ($_GET["userid"]!="" && ($_GET["userid"]!=$_SESSION["nexus_userid"])){
   $sql = "UPDATE nexus_users SET nViews=nViews+1 WHERE nUserId=".$_GET["userid"];
   $result = mysql_query($sql,$con);
}

$sql = "SELECT * FROM nexus_users WHERE nUserId=".$_GET["userid"];
$result = mysql_query($sql,$con);

 

 

if ($nUserId->cStatus==p) {$tb_stat="Active PRO Member";}
else $tb_stat="ACTIVE FREE Member";

echo $tb_stat;

Link to comment
Share on other sites

The function mysql_query() only returns a pointer to the selected records, not the records themselves. To get the records, you need to use one of the mysql fetch function. My personal preference is mysql_fetch_assoc().

 

It this code snippet:

<?php
if ($nUserId->cStatus==p) {$tb_stat="Active PRO Member";}
else $tb_stat="ACTIVE FREE Member";

echo $tb_stat;
?>

where is "$nUserId->cStatus" coming from? I don't see it being set anywhere. Also, you should always enclose all literal strings in quotes. The "if" statement should read:

<?php
if ($nUserId->cStatus == 'p')
    $tb_stat="Active PRO Member";
else
    $tb_stat="ACTIVE FREE Member";
?>

 

Ken

 

Link to comment
Share on other sites

Hi, latest test..

 


<?php

include "session.php";
include "config.php";
include "database.php";

$con = db_connect();

if ($_GET["userid"]!="" && ($_GET["userid"]!=$_SESSION["nexus_userid"])){
$sql = "UPDATE nexus_users SET nViews=nViews+1 WHERE nUserId=".$_GET["userid"];
$result = mysql_query($sql,$con);
}

$sql = "SELECT nUserId, cUsername, cStatus FROM nexus_users WHERE nUserId=".$_GET["userid"];
$result = mysql_query($sql,$con);



$result = mysql_query($sql);

if (!$result) {
   echo "Could not successfully run query ($sql) from DB: " . mysql_error();
   exit;
}

if (mysql_num_rows($result) == 0) {
   echo "No rows found, nothing to print so am exiting";
   exit;
}


while ($row = mysql_fetch_assoc($result)) {
   echo $row["nUserId"] ;
   echo $row["cUsername"];
   echo $row["cStatus"];
}

if ($cStatus ='p'){
$tb_stat="Active PRO Member";
}else{
$tb_stat="ACTIVE FREE Member";
}

mysql_free_result($result);

echo $tb_stat;

?>

 

Showing the following output for member 2 :

 



2 hboyander f

Active PRO Member

 

The 2 hboyander f is correct! but then it goes wrong again with Active PRO Member which should be ACTIVE FREE Member - I really dont get it...

 

Anyway thanks for your constructive replies,

 

Henrik

 

Link to comment
Share on other sites

You either have to set "$cStatus" to $row['cStatus'] or use the array value directly in the "if" statement:

<?php
$cStatus = $row['cStatus'];
if ($cStatus ='p'){
$tb_stat="Active PRO Member";
}else{
$tb_stat="ACTIVE FREE Member";
}
?>

or

<?php
if ($row['cStatus'] ='p'){
$tb_stat="Active PRO Member";
}else{
$tb_stat="ACTIVE FREE Member";
}
?>

 

Ken

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.