【问题标题】:Receiving Parse Error - PHP expecting identifier (T_STRING) or variable (T_VARIABLE) [duplicate]接收解析错误 - PHP 期望标识符(T_STRING)或变量(T_VARIABLE)[重复]
【发布时间】:2019-03-04 19:13:08
【问题描述】:

不太清楚我哪里出错了。我正在尝试创建一个论坛,但一直收到此错误消息。

“解析错误:语法错误,意外'[',期望标识符(T_STRING)或变量(T_VARIABLE)或'{'或'$'在C:\xampp\htdocs\project\includes\User.php在线17"

到目前为止我所拥有的:

User.php 页面:

<?php
class User {
private $user;
private $con;

public function __construct($con, $user){
    $this->con = $con;
    $user_details_query = mysqli_query($con, "SELECT * FROM users WHERE user_name='$user'");
    $this->user = mysqli_fetch_array($user_details_query);
}

public function getUsername() {
    return $this->user['user_name'];
}

public function getNumPosts() {
    $username = $this->['user_name'];
    $query = mysqli_query($this->con, "SELECT num_posts FROM users WHERE user_name='$username'");
    $row = mysqli_fetch_array($query);
    return $row['num_posts'];
}

}

?>

Post.php 页面:

<?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($topic, $title, $body, $user_to){
    $topic = strip_tags($title);
    $topic = mysqli_real_escape_string($this->con, $title);
    $title = strip_tags($title);
    $title = mysqli_real_escape_string($this->con, $title);
    $body = strip_tags($body);
    $body = mysqli_real_escape_string($this->con, $body);
    $check_empty = preg_replace('/\s+/', '', $body, $title);

    if(isset($_POST['postbtn'])){
        $title = $_POST['post_title'];
        $body = $_POST['post_text'];
        $topic = $_POST['topic'];
    }

    if($check_empty != "") {


        $date_added = date("Y-m-d H:i:s");

        $added_by = $this->user_obj->getUsername();


        if($user_to == $added_by) {
            $user_to = "none";
        }


        $query = mysqli_query($this->con, "INSERT INTO posts (post_id, topic_id, post_title, post_body, added_by, user_to, date_added, user_closed, deleted, likes) VALUES('', '$topic', '$title', '$body', '$added_by', '$user_to', '$date_added', 'no', 'no', '0')");
        $returned_id = mysqli_insert_id($this->con);


        $num_posts = $this->user_obj->getNumPosts();
        $num_posts++;
        $update_query = mysqli_query($this->con, "UPDATE users SET num_posts='$num_posts' WHERE user_name='$added_by'");
    }
}

}

?>

【问题讨论】:

  • $this-&gt;['user_name']; 语法不正确。
  • 警告:您对SQL Injections 持开放态度,应该真正使用参数化的prepared statements,而不是手动构建查询。它们由PDOMySQLi 提供。永远不要相信任何类型的输入,尤其是来自客户端的输入。即使您的查询仅由受信任的用户执行,you are still in risk of corrupting your data
  • $this-&gt;['user_name']; 本来是$this-&gt;user['user_name'];

标签: php mysqli parse-error


【解决方案1】:

首先,如 cmets 所述,您应该使用准备好的语句。您对 SQL 注入持开放态度。

其次,对于您收到的错误,在您的班级中,您将 $user 变量设置为查询的结果数组。要访问该数组,您应该在 getNumPosts() 函数中执行类似的操作:

$usernamearray = $this->user;
$username = $usernamearray['user_name'];

【讨论】:

    猜你喜欢
    • 2015-06-26
    • 2021-09-13
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 2015-11-19
    • 1970-01-01
    • 2015-03-09
    • 1970-01-01
    相关资源
    最近更新 更多