Syntax Error on line 19
Hello im writing some PHP and HTML but im not sure whats going on an error keeps coming up saying:
Parse error: syntax error, unexpected 'else' (T_ELSE) in C:wampwwwadminindex.php on line 19"
here is my code
<?php
session_start();
if(!isset($_SESSION['user_id'])){
header('Location: login.php');
exit();
}
include ('../includes/db_connect.php');
$post_count = $db->query("SELECT * FROM posts");
$comment_count = $db->query("SELECT * FROM comments");
if(isset($_POST['submit'])){
$newCategory = $_POST['newCategory'];
if(!empty($newCategory)){
$sql = "INSERT INTO categories (category) VALUES('".$newCategory."')";
$query = $db->query($sql);
if($query){
echo "New Category Added!";
}else{
echo "Error!";
//Here is where the error is happening}else{
echo 'Missing newCategory';
}
}
Any ideas on what ive done wrong?
There are a few things wrong:
Your conditional statement if(isset($_POST['submit'])){
is missing a closing brace, plus an else
was misplaced.
if(isset($_POST['submit'])){
$newCategory = $_POST['newCategory'];
if(!empty($newCategory)){
$sql = "INSERT INTO categories (category) VALUES('".$newCategory."')";
$query = $db->query($sql);
if($query){
echo "New Category Added!";
}
else{
echo "Error!";
//Here is where the error is happening
}
}
else{
echo 'Missing newCategory';
}
} // <- missing brace right there
There is a problem here:
if($query){
echo "New Category Added!";
There is a } missing.
You have some strange indendation.
You have two else
's to the same if
. Add a }
to get to the outer scope.
上一篇: 解析错误:语法错误,意外的'?' 错误
下一篇: 第19行的语法错误