删除错误用户的删除按钮?

时间:2011-08-03 03:50:44

标签: php

我目前有一个留言簿风格的东西,用户发表评论。他们的用户名在登录时存储在cookie中,当提交帖子时,他们的用户名与他们的帖子一起存储在mySQL数据库中。我正在尝试删除注释旁边的删除按钮,如果登录的用户不是发布它的用户。这是非工作代码:

        <?php
        $username = $_COOKIE['sqlusername'];
        mysqlLogin();

        $sql = mysql_query("SELECT * FROM `posts`");
        $sqlCnt = mysql_num_rows($sql);

        if($sqlCnt != 0) {
            echo "<table align='center'><tr><td class='tdno'><u><H4>Message</H4></u></td><td class='tdno'><u><H4>Poster</H4></u></td><td class='tdno'><u><H4>Time</H4></u></td></tr>";
            while($row = mysql_fetch_array($sql)) {
                if($row['username'] != $username) {
                    echo "<script type=\"text/javascript\">";
                    echo "document.getElementById('delete').innerHTML = \"\";";
                    echo "</script>";
                }
                $id = $row['id'];
                echo "<form action='delete.php' method='POST'>";
                echo "<tr><td>";
                echo $row['message'];
                echo "</td><td>";
                echo $row['poster'];
                echo "</td><td align='center' width='10'>";
                echo $row['date'];
                echo "<td align='left' width='1'>";
                echo "<input type='hidden' name='id' value='$id'>";
                echo "<span id='delete'><input type='submit' class='submit' value='Delete'></span>";
                echo "</td></tr>";
                echo "</form>";
            }
            echo "</table>";
        } else {
            echo "<div align='center'>Sorry, no posts found!</div>";
        }
    ?>

有什么想法吗?

2 个答案:

答案 0 :(得分:2)

尝试:

          if ($username == $row['poster']) {
            echo "<input type='hidden' name='id' value='$id'>";
            echo "<span id='delete'><input type='submit' class='submit' value='Delete'></span>";
          } else {
            echo " "
          }

答案 1 :(得分:-1)

也许是你的浏览器和javascript的问题。试试这个:

if (document.getElementById) { // DOM3 = IE5, NS6
        document.getElementById(id).style.display = 'none';
    }
    else {
        if{ // IE 4
            document.all.id.style.display = 'none';
        }
    }

否则您的逻辑看起来很好(假设您正在存储用户名并在数据库中正确发布。)