我想对PHP嵌套if
/ else
提供一些帮助。似乎我的代码中的最后else
应检查用户是否已登录,这给了我期待的语句错误。整个代码很好但是最后else
。
这是页面上的错误:
解析错误:语法错误,意外'否则'第141行的create_topic.php中的(T_ELSE)
虽然我理解这是因为现在的最后一个被视为额外的else
,我似乎无法弄清楚为什么因为我使用我的IDE来仔细检查我的陈述似乎都是平衡的。
<?php include "_class/core.php"; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Promanage</title>
<link rel="stylesheet" href="_css/style.css" type="text/css" />
</head>
<body>
<div id="main">
<?php
if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['Username']))
{
$username = $_SESSION['Username'];
?>
<h1>Profile</h1>
<a href="index.php" rel="nofollow">Home</a> <a href="profile.php" rel="nofollow">Profile</a> <a href="logout.php" rel="nofollow">Logout</a> <br /><br />
<?php
if($_SERVER['REQUEST_METHOD'] != 'POST')
{
$sql = "SELECT
course_id,
course_name,
course_description
FROM
course";
$result = mysql_query($sql);
if(!$result)
{
//the query failed, uh-oh :-(
echo 'Error while selecting from database. Please try again later.';
}
else
{
if(mysql_num_rows($result) == 0)
{
if($_SESSION['user_level'] == 1)
{
echo 'You have not created courses yet.';
}
else
{
echo 'Before you can post a topic, you must wait for an admin to create some categories.';
}
}
else
{
echo '<form method="post" action="">
Subject: <input type="text" name="topic_subject" />
Category:';
echo '<select name="topic_cat">';
while($row = mysql_fetch_assoc($result))
{
echo '<option value="' . $row['course_id'] . '">' . $row['course_name'] . '</option>';
}
echo '</select>';
echo 'Message: <textarea name="post_content" /></textarea>
<input type="submit" value="Create topic" />
</form>';
}
}
}
else
{
$query = "BEGIN WORK;";
$result = mysql_query($query);
if(!$result)
{
echo 'An error occured while creating your topic. Please try again later.';
}
else
{
$sql = "INSERT INTO
topics(topic_subject,
topic_date,
topic_cat,
topic_by)
VALUES('" . mysql_real_escape_string($_POST['topic_subject']) . "',
NOW(),
" . mysql_real_escape_string($_POST['topic_cat']) . ",
" . $_SESSION['user_id'] . "
)";
$result = mysql_query($sql);
if(!$result)
{
echo 'An error occured while inserting your data. Please try again later.' . mysql_error();
$sql = "ROLLBACK;";
$result = mysql_query($sql);
}
else
{
$topicid = mysql_insert_id();
$sql = "INSERT INTO
posts(post_content,
post_date,
post_topic,
post_by)
VALUES
('" . mysql_real_escape_string($_POST['post_content']) . "',
NOW(),
" . $topicid . ",
" . $_SESSION['user_id'] . "
)";
$result = mysql_query($sql);
if(!$result)
{
echo 'An error occured while inserting your post. Please try again later.' . mysql_error();
$sql = "ROLLBACK;";
$result = mysql_query($sql);
}
else
{
$sql = "COMMIT;";
$result = mysql_query($sql);
echo 'You have successfully created <a href="topic.php?id='. $topicid . '">your new topic</a>.';
}
}
}
}
}
?>
<?php
else
{
?>
<h1>Member Login</h1>
<p>Thanks for visiting! Please either login below, or <a href="register.php">click here to register</a>.</p>
<form method="post" action="index.php" name="loginform" id="loginform">
<fieldset>
<label for="username">Username:</label><input type="text" name="username" id="username" /><br />
<label for="password">Password:</label><input type="password" name="password" id="password" /><br />
<input type="submit" name="login" id="login" value="Login" />
</fieldset>
</form>
<?php
}
?>
</div>
</body>
</html>
这是发出错误的确切部分。
<?php
else
{
?>
<h1>Member Login</h1>
<p>Thanks for visiting! Please either login below, or <a href="register.php">click here to register</a>.</p>
<form method="post" action="index.php" name="loginform" id="loginform">
<fieldset>
<label for="username">Username:</label><input type="text" name="username" id="username" /><br />
<label for="password">Password:</label><input type="password" name="password" id="password" /><br />
<input type="submit" name="login" id="login" value="Login" />
</fieldset>
</form>
<?php
}
?>
答案 0 :(得分:3)
您的代码可以简化为:
<div>
<?php
if(true)
{
}
?>
<?php
else
{
}
?>
</div>
问题是PHP认为你只有一个没有if
语句的else
。然后,当它到达else
时,它无法做任何事情。这就是你得到错误的原因:
Parse error: syntax error, unexpected T_ELSE in
解决方案是将右括号移动到包含else
语句的块:
<div>
<?php
if(true)
{
?>
<?php
}
else
{
}
?>
</div>