我的mysql数据库中的数据显示不正确

时间:2015-08-24 12:23:30

标签: php mysql networking notifications social

我该如何操作它只显示所有只有一个消息的消息,而另一个问题是,当用户读取消息时,下面的这部分代码没有显示。您可以在页面底部找到完整的代码。

此代码部分未在用户阅读新消息后显示,但我不会显示:

    $newpm = '<div id="notificationTitle">Message</div>
    <div id="notificationsBody" class="notifications">You have no new messages</div>'; 

完整代码:

    $newpm_sql = mysql_query("SELECT * FROM `pm` 
                              WHERE `to` = '". $_SESSION['id'] ."' 
                              ORDER BY `id` DESC") or die(mysql_error());

    if (mysql_num_rows($newpm_sql) == 0) { 
        $newpm = '<div id="notificationTitle">Message</div>
        <div id="notificationsBody" class="notifications">You have no new messages</div>'; 
    } else {
        while ( $row = mysql_fetch_array( $newpm_sql )) {

            $from_sql = mysql_query("SELECT * FROM `members` 
                                     WHERE `id` = '". $newpm_sql['from'] ."'") 
                   or die(mysql_error());
            $from = mysql_fetch_array($from_sql);

            if ($row['status'] == 0) { 
                $newpm = '<div id="notificationTitle">Message</div>
                          <div id="notificationsBody" notifications">
<b><a href="page.php?name=profile&id='. $row['from'] .'">'. $row['subject'] .'</a></b><br> '. $row['text'] .'
                           '</div>'; 
            }
        }
    }

3 个答案:

答案 0 :(得分:1)

我看到上次错过的错误。

本声明

$newpm = '<div id="notificationTitle">Message</div>
<div id="notificationsBody" class="notifications">You have no new messages</div>'; 

正在while循环中运行,因此您应该使用$newpm将这些多次迭代连接到.=,而不仅仅是=

    while ( $row = mysql_fetch_array( $newpm_sql )) {

        $from_sql = mysql_query("SELECT * FROM `members` 
                                 WHERE `id` = '". $newpm_sql['from'] ."'") 
               or die(mysql_error());
        $from = mysql_fetch_array($from_sql);

        if ($row['status'] == 0) { 


            //$newpm = '<div id="notificationTitle">Message</div>
            newpm .= '<div id="notificationTitle">Message</div>      
                      <div id="notificationsBody" notifications">
                         <b>
                            <a href="page.php?name=profile&id='. $row['from'] .'">'. $row['subject'] .'</a>
                        </b>
                        <br> '. 
                        $row['text'] .'
                      '</div>'; 
        }
    }

答案 1 :(得分:0)

首先运行:

SELECT * FROM `pm` WHERE `to` = '<your SESSION ID>'
ORDER BY `id` DESC

并查看是否返回了某些内容。

你也可以这样做:

var_dump(mysql_num_rows($newpm_sql));die;  

在第一行运行此mysql_query后,看看它是否返回了您期望的内容。

此外,您可以切换到PDO - 您不会后悔,因为您现在使用旧的PHP功能。

答案 2 :(得分:0)

文件的整个代码(page.php):

 <?php
    session_start();
    include('pages/config.php');


    if (!isset($_SESSION['username']))
    { header('Location: index.php'); }


    mysql_query("UPDATE `members` SET `active_time` = NOW() WHERE `id` = '". $_SESSION['id'] ."'") or die(mysql_error());


    mysql_query("UPDATE `members` SET `status` = '0' WHERE `active_time` < DATE_SUB(now(), INTERVAL 30 MINUTE)") or die(mysql_error());
    if (isset($_SESSION['username']))
    { mysql_query("UPDATE `members` SET `status` = '1' WHERE `id` = '". $_SESSION['id'] ."'"); }

    ini_set('display_errors',0);

    if (!isset($_GET['name']) or trim(empty($_GET['name'])))
    { $name = 'welcome'; }

    else
    { $name = mysql_real_escape_string($_GET['name']); }

    $page_sql = mysql_query("SELECT * FROM `pages` WHERE `name` = '". $name ."'") or die(mysql_error());
    $page = mysql_fetch_array($page_sql);




        $newpm_sql = mysql_query("SELECT * FROM `pm` WHERE `to` = '". $_SESSION['id'] ."' ORDER BY `id` DESC") or die(mysql_error());

        if (mysql_num_rows($newpm_sql) == 0) { 
            $newpm = 'You have no new messages'; 
        } else {
            while ( $row = mysql_fetch_array( $newpm_sql )) {

                $from_sql = mysql_query("SELECT * FROM `members` 
                                         WHERE `id` = '". $newpm_sql['from'] ."'") 
                       or die(mysql_error());
                $from = mysql_fetch_array($from_sql);

                if ($row['status'] == 0) { 
                    $newpm = '<b><a href="page.php?name=profile&id='. $row['from'] .'">'. $row['subject'] .'</a></b><br> '. $row['text'] .'
                               '; 
                }
            }
        }


    $userinfo_sql = mysql_query("SELECT * FROM `members` WHERE `id` = '". $_SESSION['id'] ."'") or die(mysql_error());
    $userinfo = mysql_fetch_array($userinfo_sql);

    if (empty($userinfo['picture']))
    { $picture = 'images/nothing.png'; }
    elseif (!empty($userinfo['picture']))
    { $picture = $userinfo['picture']; }

    if ($userinfo['access'] >= 1)
    { $page_admin = '<li><a href="page.php?name=admin">Administration</a></li>'; }



    function bbcode($text)
    {


    $regexp = array( 
    "/\[url=(http:\/\/)?(.+)\](.+)\[\/url\]/iU" => "<a href=\"http://$2\" target=\"_blank\">$3</a>", // [url=][/url] 
    "/\[url\](http:\/\/)?(.+)\[\/url\]/iU" => "<a href=\"http://$2\" target=\"_blank\">$2</a>", // [url][/url] 
    "/\[img\](http:\/\/)?(.+)\[\/img\]/iU" => "<img src=\"http://$2\" border=\"0\" alt=\"\" />", // [img][/img] 
    "/\[email\]([a-z0-9\.-_]+@[a-z0-9]+\.[a-z0-9]+)\[\/email\]/iU" => "<a href=\"mailto:$1\">$1</a>", // [email][/email] 
    "/\[b\](.+)\[\/b\]/iU" => "<strong>$1</strong>", // [b][/b] 
    "/\[s\](.+)\[\/s\]/iU" => "<del>$1</del>", // [s][/s] 
    "/\[i\](.+)\[\/i\]/iU" => "<em>$1</em>", // [i][/i] 
    "/\[u\](.+)\[\/u\]/iU" => "<ins>$1</ins>", // [u][/u] 
    "/\[hr\]/i" => "<hr />", // [hr] 
    "/\[quote\](.+)\[\/quote\]/iU" => "<p><strong>Citat:</strong> \"$1\"</p>", // [quote][/quote] 
    "/\[quote=(.+)\](.+)\[\/quote\]/iU" => "<p><strong>$1 skrev:</strong> \"$2\"</p>", // [quote=][/quote] 
    "/\[blink\](.+)\[\/blink\]/iU" => "<span style=\"text-decoration: blink;\">$1</span>", // [blink][/blink] 
    "/\[marquee\](.+)\[\/marquee\]/iU" => "<marquee>$1</marquee>", // [marquee][/marquee] 
    "/\[center\](.+)\[\/center\]/iU" => "<div style=\"text-align: center;\">$1</div>", // [center][/center] 
    "/\[right\](.+)\[\/right\]/iU" => "<div style=\"text-align: right;\">$1</div>", // [right][/right] 
    "/\[left\](.+)\[\/left\]/iU" => "<div style=\"text-align: left;\">$1</div>", // [left][/left] 
    "/\[color=darkred\](.+)\[\/color\]/iU" => "<span style=\"color: darkred;\">$1</span>", 
    "/\[color=red\](.+)\[\/color\]/iU" => "<span style=\"color: red;\">$1</span>", 
    "/\[color=orange\](.+)\[\/color\]/iU" => "<span style=\"color: orange;\">$1</span>", 
    "/\[color=brown\](.+)\[\/color\]/iU" => "<span style=\"color: brown;\">$1</span>", 
    "/\[color=yellow\](.+)\[\/color\]/iU" => "<span style=\"color: yellow;\">$1</span>", 
    "/\[color=green\](.+)\[\/color\]/iU" => "<span style=\"color: green;\">$1</span>", 
    "/\[color=olive\](.+)\[\/color\]/iU" => "<span style=\"color: olive;\">$1</span>", 
    "/\[color=cyan\](.+)\[\/color\]/iU" => "<span style=\"color: cyan;\">$1</span>", 
    "/\[color=blue\](.+)\[\/color\]/iU" => "<span style=\"color: blue;\">$1</span>", 
    "/\[color=darkblue\](.+)\[\/color\]/iU" => "<span style=\"color: darkblue;\">$1</span>", 
    "/\[color=indigo\](.+)\[\/color\]/iU" => "<span style=\"color: indigo;\">$1</span>", 
    "/\[color=violet\](.+)\[\/color\]/iU" => "<span style=\"color: violet;\">$1</span>", 
    "/\[color=white\](.+)\[\/color\]/iU" => "<span style=\"color: white;\">$1</span>", 
    "/\[color=black\](.+)\[\/color\]/iU" => "<span style=\"color: black;\">$1</span>", 
    "/\[size=1\](.+)\[\/size\]/iU" => "<span style=\"font-size: 18px;\">$1</span>", 
    "/\[size=2\](.+)\[\/size\]/iU" => "<span style=\"font-size: 24px;\">$1</span>", 
    "/\[size=3\](.+)\[\/size\]/iU" => "<span style=\"font-size: 32px;\">$1</span>", 
    "/\[size=4\](.+)\[\/size\]/iU" => "<span style=\"font-size: 48px;\">$1</span>", 
    "/\[size=\-1\](.+)\[\/size\]/iU" => "<span style=\"font-size: 12px;\">$1</span>", 
    "/\[size=\-2\](.+)\[\/size\]/iU" => "<span style=\"font-size: 10px;\">$1</span>", 
    "/\[youtube\](.+)\[\/youtube\]/iU" => "<object width=\"425\" height=\"344\"><param name=\"movie\" value=\"http://www.youtube.com/v/$1\"></param><param name=\"allowFullScreen\" value=\"true\"></param><embed src=\"http://www.youtube.com/v/$1\" type=\"application/x-shockwave-flash\" allowfullscreen=\"true\" width=\"425\" height=\"344\"></embed></object>", // [youtube][/youtube] 
    "/\[code\](.+)\[\/code\]/eU" => "highlight_string(\"$1\", true)" // [code][/code] 
    ); 

    foreach ($regexp as $regexps => $regexp_replacements) 
    { $text = preg_replace($regexps, $regexp_replacements, $text); }

    return $text;

    }



    $page_sidemenu = '

    <li id="sidemenu-topli">


    <table cellpadding="0" cellspacing="0" style="width: 100%">
        <tr>
            <td><b>Välkommen, </b>'. $userinfo['username'] .'!</td>
        </tr>
        <tr>
            <td>
            <table cellpadding="0" cellspacing="0" style="width: 100%">
                <tr>
                    <td><img class="pagePicture" src="'. $userinfo['picture'] .'" height="85" width="80" /></td>
                    <td><a href="page.php?name=guestbook&id='. $userinfo['id'] .'">Gästbok</a>
                    <br><a href="page.php?name=picture&id='. $userinfo['id'] .'">Fotoalbum</a>
                    <br><a href="page.php?name=blog&id='. $userinfo['id'] .'">Blogg</a>
                    </td>
                </tr>
            </table>
            </td>
        </tr>
    </table>





    </li>
    <li id="sidemenu-li"><a href="page.php?name=forum">Forum</a></li>
    <li id="sidemenu-li"><a href="page.php?name=gallery">Galleri</a></li>
    <li id="sidemenu-li"><a href="page.php?name=userlist">Medlemmar</a></li>
    <li id="sidemenu-li"><a href="page.php?name=livemess">Livemess</a></li>





    ';


    $livemess_sql = mysql_query("SELECT `userid`, `message` FROM `livemess` ORDER BY `id` DESC LIMIT 0,1") or die(mysql_error());
    $livemess = mysql_fetch_array($livemess_sql);

    $livemessuser_sql = mysql_query("SELECT * FROM `members` WHERE `id` = '". $livemess['userid'] ."'") or die(mysql_error());
    $livemessuser = mysql_fetch_array($livemessuser_sql);

    if (empty($livemessuser['picture']))
    { $livemesspicture = 'images/nothing.png'; }
    else
    { $livemesspicture = $livemessuser['picture']; }




    $page_menu = 
    '
    <div id="header">




    <div class="top-bar">
                <div class="top-bar-container">
                    <ul id="nav">
                        <li><a href="#">Home</a></li>
                        <li><a href="#">About</a></li>
                        <li id="notification_li">
                            <span id="notification_count">3</span>
                            <a href="#" id="notificationLink">Meddelande</a>
                            <div id="notificationContainer">
                              <div id="notificationTitle">Meddelande</div>
        <div id="notificationsBody" class="notifications">  '. $newpm .' </div>  


                                <div id="notificationFooter"><a href="#">Visa alla</a></div>
                            </div>

                        </li>
                        <li><a href="#">Logout</a></li>
                    </ul>
                </div>
            </div>



    <div id="subbar">
    <div class="subbar-divtable">

    <div class="left"><img class="subbar-img" src="'. $livemesspicture .'" border="0" width="60" height="60" /></div>
    <div class="right"><b><a style="font-size: 15px;" href="page.php?name=profile&id='. $livemessuser['id'] .'">'. $livemessuser['username'] .'</a> </b><br><br>'. htmlspecialchars($livemess['message']) .'</div>

    </div>
    </div>
    ';



    $page_subject = $page['subject'];
    $page_content = $page['url'];


    if ($userinfo['banned'] == 1 and $name != 'logout')
    { echo "<script type='text/javascript'> alert('Ditt konto är spärrat från ". $sitename .".'); </script>"; exit; }

    else
    {

    if (mysql_num_rows($page_sql) == 0)
    { echo "<script type='text/javascript'> alert('Sidan du söker efter kan inte hittas.'); </script>"; exit; }

    }


    $design_sql = mysql_query("SELECT `folder` FROM `styles` WHERE `active` = '1'") or die(mysql_error());
    $design = mysql_fetch_array($design_sql);


    ?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <!--
    Design by Free CSS Templates
    http://www.freecsstemplates.org
    Released for free under a Creative Commons Attribution 2.5 License

    Name       : Compromise
    Description: A two-column, fixed-width design with dark color scheme.
    Version    : 1.0
    Released   : 20081103

    -->
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta name="keywords" content="" />
    <meta name="description" content="" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title><?php echo $sitename; ?></title>
    <link href="styles/<?php echo $design['folder']; ?>/style.css" rel="stylesheet" type="text/css" media="screen" />
    <script type="text/javascript" src="styles/<?php echo $design['folder']; ?>/js/jquery.min.js"></script>
    <script type="text/javascript" >
    $(document).ready(function ()
    {
        $("#notificationLink").click(function ()
        {
            $("#notificationContainer").fadeToggle(300);
            $("#notification_count").fadeOut("slow");
            return false;
        });


        $(document).click(function ()
        {
            //hide notification popup on doucument click
            $("#notificationContainer").hide();
        });


        $("#notificationContainer").click(function ()
        {
            return false;
        });

    });
    </script>

    </head>
    <body>
    <?php include('styles/'. $design['folder'] .'/body.php'); ?>

    <div id="footer">
            <p>Copyright <?php echo $sitename .' '. date('Y'); ?>. Design av <a href="#">
            Gr4x</a>.</p>
        </div>

    </body>
    </html>