我在尝试基于UserLevel显示不同的菜单选项时遇到问题。我有一个带有用户表的mysql数据库。 users表包含一个UserLevel,它将被设置为0或1.但由于某种原因,我的php无法正常工作。事实上,当我将php添加到菜单中时,它不会在菜单下方的网站上显示任何内容。任何建议都将不胜感激。
<?php
if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['Username']))
{
?>
<?php include "mainNav.php"; ?>
<center>
<h2> Campaign Updates</h2>
</center>
<div id="campaignPostWrap">
<div id="campaignScrollBox">
<?php
$con=mysqli_connect("localhost","dorians","ds2953!b67P$","aldentec");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM campaigns ORDER BY postDate desc");
while($row = mysqli_fetch_array($result))
{
echo "<div id='campaignPostContainer'>";
echo "<ul class='campaignPostBox'>";
echo "<p class='postInfo'>";
echo "Posted on:";
echo "<li>" . $row['postDate'] . "</li>";
echo "</p>";
echo "<p class='postInfo'>";
echo "Posted by:";
echo "<li>" . $row['postName'] . "</li>";
echo "</p>";
echo "<li class='postEntry'>" . $row['postEntry'] . "</li>";
echo "</ul>";
echo "</div>";
echo "<hr>";
}
mysqli_close($con);
?>
</div>
<?php include "campaignPost.php"; ?>
</div>
<?php include "chat.php"; ?>
<?php
}
elseif(!empty($_POST['username']) && !empty($_POST['password']))
{
$username = mysql_real_escape_string($_POST['username']);
$password = md5(mysql_real_escape_string($_POST['password']));
$checklogin = mysql_query("SELECT * FROM users WHERE Username = '".$username."' AND Password = '".$password."'");
if(mysql_num_rows($checklogin) == 1)
{
$row = mysql_fetch_array($checklogin);
$email = $row['EmailAddress'];
$userlevel = $row['UserLevel'];
$_SESSION['Username'] = $username;
$_SESSION['EmailAddress'] = $email;
$_SESSION['LoggedIn'] = 1;
$_SESSION['UserLevel'] = $userlevel;
echo "<h1>Success</h1>";
echo "<p>We are now redirecting you to the member area. If you are not automatically redirected <a href='index.php'>Click here</a></p>";
header( "refresh:10;url=index.php" );
}
else
{
echo "<h1>Error</h1>";
echo "<p>Sorry, your account could not be found. Please <a href=\"index.php\">click here to try again</a>.</p>";
}
}
else
{
?>
<?php session_start(); ?>
<?php
$userlevel = $_SESSION['UserLevel'];
if($userlevel == 0) {
echo "<ul class="mainNav">
<a href="characterRoll.php"><li> Create Character</li></a>
<a href="questCreate.php"><li> Create Quest</li></a>
<a href="characterView.php"><li> View Characters</li></a>
<a href="questList.php"><li> View Quests</li></a>
<a href="booksView.php"><li> Book List</li></a>
</ul>";
} elseif($userlevel == 1) {
echo "<a href="dmTools.php"><li> DM Tools</li></a>";
}
?>
<?php include "greeter.php"; ?>
答案 0 :(得分:3)
你的报价无疑是问题所在:
if($userlevel == 0) {
echo "<ul class="mainNav">
<a href="characterRoll.php"><li> Create Character</li></a>
<a href="questCreate.php"><li> Create Quest</li></a>
<a href="characterView.php"><li> View Characters</li></a>
<a href="questList.php"><li> View Quests</li></a>
<a href="booksView.php"><li> Book List</li></a>
</ul>";
} elseif($userlevel == 1) {
echo "<a href="dmTools.php"><li> DM Tools</li></a>";
}
请注意,上面的语法突出显示了字符串中的问题。看看它到达mainNav
时会变成黑色?那是因为mainNav
不再是字符串的一部分。这是件坏事。
查看echo
的第一行:
echo "<ul class="mainNav">
您打开报价然后在class="
关闭它。现在,它正在尝试将mainNav
评估为常量或其他语言构造。最重要的是,它不知道如何处理mainNav
,因为您没有提供任何类型的运算符。
相反,你应该做类似的事情:
if($userlevel == 0) {
echo '<ul class="mainNav">
<a href="characterRoll.php"><li> Create Character</li></a>
<a href="questCreate.php"><li> Create Quest</li></a>
<a href="characterView.php"><li> View Characters</li></a>
<a href="questList.php"><li> View Quests</li></a>
<a href="booksView.php"><li> Book List</li></a>
</ul>';
} elseif($userlevel == 1) {
echo '<a href="dmTools.php"><li> DM Tools</li></a>';
}
或者,您可以转义存在非字符串终止引用的每个位置,例如\"
。
另一种选择是使用Heredoc语法。