0

Hi! I am building a simple blog website and am stuck when I get to the posting comments part. I write my comment to post and when I hit post comment I get the error message:


Parse error: syntax error, unexpected T_ELSE in C:\xampp\htdocs\simple_blog\inc\update.inc.php on line 90

Here is my code so far for update.inc.php:

<?php

// Include the functions so we can create a URL
include_once 'functions.inc.php';

if($_SERVER['REQUEST_METHOD']=='POST'
    && $_POST['submit']=='Save Entry'
    && !empty($_POST['page'])
    && !empty($_POST['title'])
    && !empty($_POST['entry']))
{
    // Create a URL to save in the database
    $url = makeUrl($_POST['title']);

    // Include database credentials and connect to the database
    include_once 'db.inc.php';
    $db = new PDO(DB_INFO, DB_USER, DB_PASS);

    // Edit an existing entry
    if(!empty($_POST['id']))
    {
        $sql = "UPDATE entries
                SET title=?, entry=?, url=?
                WHERE id=?
                LIMIT 1";
        $stmt = $db->prepare($sql);
        $stmt->execute(
            array(
                $_POST['title'],
                $_POST['entry'],
                $url,
                $_POST['id']
            )
        );
        $stmt->closeCursor();
    }

    // Create a new entry
    else 
    {
        // Save the entry into the database
        $sql = "INSERT INTO entries (page, title, entry, url)
                VALUES (?, ?, ?, ?)";
        $stmt = $db->prepare($sql);
        $stmt->execute(
            array(
                $_POST['page'],
                $_POST['title'],
                $_POST['entry'],
                $url
            )
        );
        $stmt->closeCursor();
    }

    // Sanitize the page information for use in the success URL
    $page = htmlentities(strip_tags($_POST['page']));

    // Send the user to the new entry
    header('Location: /simple_blog/'.$page.'/'.$url);
    exit;
}
// If a comment is being posted, handle it here
else if($_SERVER['REQUEST_METHOD'] == 'POST'
    && $_POST['submit'] == 'Post Comment')
{
    // Include and instantiate the Comments class
    include_once 'comments.inc.php';
    $comments = new Comments();

    // Save the comment
    $comments->saveComment($_POST);
	{

		// If available, store the entry the user came from
		if(isset($_SERVER['HTTP_REFERER']))
		{
			$loc = $_SERVER['HTTP_REFERER'];
		}
		else
		{	
			$loc = '../';
		}

		// Send the user back to the entry
		header('Location: '.$loc);
		exit;
	}
	//If saving fail output error message
	else
	{
		exit('Something went wrong while saving the comment.');
	}
}
else
{
    header('Location: ../');
    exit;
}

?>

Any suggestions? I have been working on it for hours :/

2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by diafol
0
<?php
 
// Include the functions so we can create a URL
include_once 'functions.inc.php';
 
if($_SERVER['REQUEST_METHOD']=='POST'
    && $_POST['submit']=='Save Entry'
    && !empty($_POST['page'])
    && !empty($_POST['title'])
    && !empty($_POST['entry']))
{
    // Create a URL to save in the database
    $url = makeUrl($_POST['title']);
 
    // Include database credentials and connect to the database
    include_once 'db.inc.php';
    $db = new PDO(DB_INFO, DB_USER, DB_PASS);
 
    // Edit an existing entry
    if(!empty($_POST['id']))
    {
        $sql = "UPDATE entries
                SET title=?, entry=?, url=?
                WHERE id=?
                LIMIT 1";
        $stmt = $db->prepare($sql);
        $stmt->execute(
            array(
                $_POST['title'],
                $_POST['entry'],
                $url,
                $_POST['id']
            )
        );
        $stmt->closeCursor();
    }
 
    // Create a new entry
    else 
    {
        // Save the entry into the database
        $sql = "INSERT INTO entries (page, title, entry, url)
                VALUES (?, ?, ?, ?)";
        $stmt = $db->prepare($sql);
        $stmt->execute(
            array(
                $_POST['page'],
                $_POST['title'],
                $_POST['entry'],
                $url
            )
        );
        $stmt->closeCursor();
    }
 
    // Sanitize the page information for use in the success URL
    $page = htmlentities(strip_tags($_POST['page']));
 
    // Send the user to the new entry
    header('Location: /simple_blog/'.$page.'/'.$url);
    exit;
}
// If a comment is being posted, handle it here
else if($_SERVER['REQUEST_METHOD'] == 'POST'
    && $_POST['submit'] == 'Post Comment')
{
    // Include and instantiate the Comments class
    include_once 'comments.inc.php';
    $comments = new Comments();
 
    // Save the comment
    $comments->saveComment($_POST);
	{ //WHAT'S THIS???? SHOULD IT BE if(...){
 
		// If available, store the entry the user came from
		if(isset($_SERVER['HTTP_REFERER']))
		{
			$loc = $_SERVER['HTTP_REFERER'];
		}
		else
		{	
			$loc = '../';
		}
 
		// Send the user back to the entry
		header('Location: '.$loc);
		exit;
	}
	//If saving fail output error message
	else
	{
		exit('Something went wrong while saving the comment.');
	}
}
else
{
    header('Location: ../');
    exit;
}
 
?>

Edited by diafol: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.