0

I keep getting the error

Parse error: syntax error, unexpected '?>', expecting function (T_FUNCTION) or const (T_CONST)

From what I read this usually points to a missing "}", but everything seems to match up no matter how many times I read over it. I also tried removing one of the "}" in case I had one too many, but it came back with the error `

Parse error: syntax error, unexpected end of file

`.

Here's my code:

<?php
class Post{ 
     private $user_obj;
     private $con;

     public function __construct($con, $user){
            $this->con = $con;
            $this->user_obj = new User($con, $user);
     }


     public function submitPost($body, $user_to) {
         $body = strip_tags($body); //removes any HTML tags             
         $body = mysqli_real_escape_string($this->con, $body); //Allows the use of " ' " so it doesn't think it's a new string. 
         $check_empty = preg_replace('/\s+/', '', $body); //Deletes all spaces

         if($check_empty != "") {


             //Current date and time
             $date_added = date("Y-m-d H:i:s");
             //Get username
             $added_by = $this->user_obj->getUsername();

             //if user is on their own profile, user_to is 'none'
             if($user_to == $added_by) {
                 $user_to = "none";
             }

             //insert post
            $query = mysqli_query($this->con, "INSERT INTO posts VALUES('', 
            '$body', '$added_by', '$user_to', '$date_added', 'no', 'no', 
            '0')");

             $returned_id = mysqli_insert_id($this->con);

             //Insert notification

             //Update post count for user
             $num_posts = $this->user_obj->getNumPosts();
             $num_posts++;
             $update_query = mysqli_query($this->con, "UPDATE users SET 
             num_posts='$num_posts' WHERE username='$added_by'");
         }
     }
?>
Pradeep
  • 9,667
  • 13
  • 27
  • 34
Angelica Silvera
  • 19
  • 1
  • 1
  • 3

4 Answers4

1

You failed to close the class Try this

    <?php
class Post{ 
     private $user_obj;
     private $con;

     public function __construct($con, $user){
            $this->con = $con;
            $this->user_obj = new User($con, $user);
}


     public function submitPost($body, $user_to) {
         $body = strip_tags($body); //removes any HTML tags
         $body = mysqli_real_escape_string($this->con, $body); //Allows the 
         use of " ' " so it doesn't think it's a new string.
         $check_empty = preg_replace('/\s+/', '', $body); //Deletes all 
         spaces

         if($check_empty != "") {


             //Current date and time
             $date_added = date("Y-m-d H:i:s");
             //Get username
             $added_by = $this->user_obj->getUsername();

             //if user is on their own profile, user_to is 'none'
             if($user_to == $added_by) {
                 $user_to = "none";
             }

             //insert post
            $query = mysqli_query($this->con, "INSERT INTO posts VALUES('', 
            '$body', '$added_by', '$user_to', '$date_added', 'no', 'no', 
            '0')");

             $returned_id = mysqli_insert_id($this->con);

             //Insert notification

             //Update post count for user
             $num_posts = $this->user_obj->getNumPosts();
             $num_posts++;
             $update_query = mysqli_query($this->con, "UPDATE users SET 
             num_posts='$num_posts' WHERE username='$added_by'");
             }
    }
}
    ?>
Siva Ganesh
  • 1,415
  • 2
  • 16
  • 28
0

Add a } at end before ?>
Because you haven't closed your Class Post

prit.patel
  • 330
  • 3
  • 12
  • In my case, it was php version issue. I switched from php7.3 to 7.4 and it worked. Hope this comment will help someone. – Faiyaz Alam Dec 17 '20 at 05:56
0

I count 9 { and } combined. If it where right they where even. You're missing a } at the end i think, but its hard to check on my phone.

Max
  • 1,536
  • 1
  • 14
  • 18
-1

Note: First of all you need to flow coding style and change your coding pattern. https://www.php-fig.org/psr/psr-1/ look this page.