Reputation: 33
I would like some help regarding PHP nested if
/else
. It seems that the last else
in my code which should check if a user is logged in are giving me expecting statement error. The whole code is fine but the last else
.
This is the error on the page:
Parse error: syntax error, unexpected 'else' (T_ELSE) in create_topic.php on line 141
While I understand it’s because the last else now is treated as an extra else
, I can’t seem to figure out why since I used my IDE to double check my statement all seems to be balanced.
<?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>
This is the exact part that gave out the error.
<?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
}
?>
Upvotes: 0
Views: 3002
Reputation: 42048
Your code can be simplified to this:
<div>
<?php
if(true)
{
}
?>
<?php
else
{
}
?>
</div>
The problem is that PHP thinks you only have an if
without an else
statement. Then when it reaches else
, it can't do anything with that. This is why you get the error:
Parse error: syntax error, unexpected T_ELSE in
The solution is to move the closing brace to the block which contains the else
statement:
<div>
<?php
if(true)
{
?>
<?php
}
else
{
}
?>
</div>
Upvotes: 3