0

in my site you can follow and unfollow some other user, i did that but I want now to give them the ability to a user (i have two types of users, doctor and patient) to see members of the other category. To make it more clear, i logged in as a doctor and i want to see all the users that belong to the patient table of my database, notice that doctor and patient belongs to differents tables of the database.

My code is this...

<?php
include_once 'header.php';

if (!$loggedin) die();

echo "<div class='main'>";

if (isset($_GET['view']))
{
    $view = sanitizeString($_GET['view']);

    if ($view == $username) $name = "Your";
    else                $name = "$view's";

    echo "<h3>$name Profile</h3>";
    /**
     * Deixnoume to profil opws to exoume ftiaksei sto function.php
     */
    showProfile($view);
     echo "<a class='button' href='messages.php?view=$view'>" .
         "View $name messages</a><br /><br />";
    die("</div></body></html>");
}
/**
* This code is for showing the users while you are logged in as patient
*/
if("SELECT username FROM patient WHERE username='$username'")
{
  if (isset($_GET['add']))
  {
    $add = sanitizeString($_GET['add']);

    if (!mysql_num_rows(queryMysql("SELECT * FROM friends
        WHERE username='$add' AND friend='$username'")))
        queryMysql("INSERT INTO friends VALUES ('$add', '$username')");
  }
  elseif (isset($_GET['remove']))
  {
    $remove = sanitizeString($_GET['remove']);
    queryMysql("DELETE FROM friends WHERE username='$remove' AND friend='$username'");
  }

    $result = queryMysql("SELECT username FROM doctor ORDER BY username");
    $num    = mysql_num_rows($result);

    echo "<h3>Other Members</h3><ul>";

    for ($j = 0 ; $j < $num ; ++$j)
    {
        $row = mysql_fetch_row($result);
        if ($row[0] == $username) continue;

        echo "<li><a href='members.php?view=$row[0]'>$row[0]</a>";
        $follow = "follow";

        $t1 = mysql_num_rows(queryMysql("SELECT * FROM friends
            WHERE username='$row[0]' AND friend='$username'"));
        $t2 = mysql_num_rows(queryMysql("SELECT * FROM friends
            WHERE username='$username' AND friend='$row[0]'"));

        if (($t1 + $t2) > 1) echo " &harr; is a mutual friend";
        elseif ($t1)         echo " &larr; you are following";
        elseif ($t2)       { echo " &rarr; is following you";
                          $follow = "recip"; }

        if (!$t1) echo " [<a href='members.php?add=".$row[0]    . "'>$follow</a>]";
        else      echo " [<a href='members.php?remove=".$row[0] . "'>drop</a>]";
    }  
}
/**
* This code is for showing the users while you are logged in as doctor
*/
elseif("SELECT username FROM doctor WHERE username='$username'")
{
  if (isset($_GET['add']))
  {
    $add = sanitizeString($_GET['add']);

    if (!mysql_num_rows(queryMysql("SELECT * FROM friends
        WHERE username='$add' AND friend='$username'")))
        queryMysql("INSERT INTO friends VALUES ('$add', '$username')");
  }
  elseif (isset($_GET['remove']))
  {
    $remove = sanitizeString($_GET['remove']);
    queryMysql("DELETE FROM friends WHERE username='$remove' AND friend='$username'");
  }

    $result = queryMysql("SELECT username FROM patient ORDER BY username");
    $num    = mysql_num_rows($result);

    echo "<h3>Other Members</h3><ul>";

    for ($j = 0 ; $j < $num ; ++$j)
    {
        $row = mysql_fetch_row($result);
        if ($row[0] == $username) continue;

        echo "<li><a href='members.php?view=$row[0]'>$row[0]</a>";
        $follow = "follow";

        $t1 = mysql_num_rows(queryMysql("SELECT * FROM friends
            WHERE username='$row[0]' AND friend='$username'"));
        $t2 = mysql_num_rows(queryMysql("SELECT * FROM friends
            WHERE username='$username' AND friend='$row[0]'"));

        if (($t1 + $t2) > 1) echo " &harr; is a mutual friend";
        elseif ($t1)         echo " &larr; you are following";
        elseif ($t2)       { echo " &rarr; is following you";
                          $follow = "recip"; }

        if (!$t1) echo " [<a href='members.php?add=".$row[0]    . "'>$follow</a>]";
        else      echo " [<a href='members.php?remove=".$row[0] . "'>drop</a>]";
    }  
}

?>

<br /></div></body></html>

I did it for the patient but it does not show me the users which are patients when i am logged in as a doctor. I think there is a problem with the if and the elseif of my code but i cant find what. Can you help me?

Waaaaat
  • 634
  • 3
  • 14
  • 29
  • 1
    Please, [don't use `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) in new code. They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://us1.php.net/pdo) or [MySQLi](http://us1.php.net/mysqli). – Jay Blanchard Dec 30 '14 at 18:40

1 Answers1

0

I found the answer and it the below... Replace the if("SELECT username FROM patient WHERE username='$username'") with this ...

$result = queryMysql("SELECT * FROM patient WHERE username='$username'");
if(mysql_num_rows($result))

and for the elseif just delete what it is inside the parenthesis and make it else

Waaaaat
  • 634
  • 3
  • 14
  • 29