我想显示存在于数据库中不同表中的站点成员

时间:2014-12-30 18:39:20

标签: php mysql

在我的网站中,您可以关注并取消关注其他用户,我这样做但我现在想让他们能够向用户(我有两种类型的用户,医生和患者)查看其他类别的成员。 为了更清楚,我以医生的身份登录,我希望看到属于我的数据库患者表的所有用户,注意医生和患者属于数据库的不同表。

我的代码就是这个......

<?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>

我为患者做了这件事,但是当我作为医生登录时,它没有向我显示患者。我认为我的代码ifelseif存在问题,但我无法找到。你能救我吗?

1 个答案:

答案 0 :(得分:0)

我找到了答案,下面是...... 将if("SELECT username FROM patient WHERE username='$username'")替换为此...

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

并且elseif只需删除括号内的内容并将其设为else