我一直收到错误
解析错误:语法错误,意外'?>',期待功能 (T_FUNCTION)或const(T_CONST)
根据我的阅读,这通常指向一个缺失的“}”,但无论我多少次阅读它,一切似乎都匹配。我也尝试删除其中一个“}”,以防我有一个太多,但它回来时出现错误`
解析错误:语法错误,意外的文件结尾
`。
这是我的代码:
<?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'");
}
}
?>
答案 0 :(得分:0)
你未能关闭课程试试这个
<?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'");
}
}
}
?>
答案 1 :(得分:0)
在}
之前添加?>
因为您尚未关闭Class Post
答案 2 :(得分:0)
我计算9 {和}合计。如果它在哪里,他们在哪里均匀。我觉得你最后错过了一个},但很难检查我的手机。
答案 3 :(得分:0)
注意:首先,您需要流式编码样式并更改编码模式。 https://www.php-fig.org/psr/psr-1/查看此页面。