简体   繁体   中英

IF statement not sure what I am doing wrong

Before I start I am learning PHP/Mysql, so I am trying to learn and understand all that before I move onto mysqli and researching correct security steps, so I am aware this code will have holes int it.

Basically I am implementing a member group system and I am trying to work out the coding so that..

IF a member is in the group, show "group member" IF a member isn't in the group, "join group" IF a member has already requested a join, but accepted is still = 0(pending) show "invite pending".

Here is my code so far, it's inside an include.

<?php

$id = $_GET['gid'];
$gruser = $_SESSION['user_id'];

$group = "SELECT * FROM `disc_users` WHERE user_id='$gruser' AND group_id='$id'";
$gres = mysql_query($group);

if ($gres == 0) { ?>
<input type="button" class="sub-button" value="Join Group">
<? 
} 
if ($gres == 1) { ?>
<input type="button" class="sub-button" value="Group Member">
<? } ?>

I can't figure out what I am doing wrong, it's not displaying errors, but I am getting white space, no buttons, I even added "echo" in front of $group and it's getting the values correctly.

Any help appreciated.

You cant simply compare mysql_query result you have to use mysql_fetch_row etc but in your case may be like this:

<?php

    $id = $_GET['gid'];
    $gruser = $_SESSION['user_id'];

    $group = "SELECT * FROM `disc_users` WHERE user_id='$gruser' AND group_id='$id'";
    $gres = mysql_query($group);

    if (mysql_num_rows($gres) < 0) { ?>
    <input type="button" class="sub-button" value="Join Group">
    <? 
    } 
    else{ ?>
    <input type="button" class="sub-button" value="Group Member">
    <? } ?>

mysql queries always return as an array its not possible to compare them in a if statement before first breaking the array down in to individual variables.

you can use mysql_num_rows, $num_rows = mysql_num_rows($gres);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM