PHP - 无法识别对象

时间:2017-03-01 20:37:48

标签: php

我有一个方法可以根据用户名从数据库中删除多个用户。如果提供了用户名或电子邮件,则可以将其用作用户名或电子邮件。我使用的是从作曲家构建的路线。从Postman测试路线时,它工作得很好,但是从我的网页调用路线时,我收到了错误

  

{" INVALID_EMAIL":[]," invalid_username":[""]}
  注意:尝试在 205

/Path/model/users.php 中获取非对象的属性>

我不确定为什么,因为我从我的User对象中提取,这在之前的测试中有效。

这是路线呼叫:

$app->delete("/multiple_users", function($request, $response, $args){
  $body = $request->getParsedBody();
  $sessionKey = $request->getQueryParams()['session_key'];
  $sessionID = $request->getQueryParams()['session_id'];
  $users = $body['users'];
  global $database;
  $statement = $database->prepare("SELECT * FROM session WHERE session_key =  AND id = ?");
  $statement->execute(array($sessionKey, $sessionID));
  if($statement->rowCount()==0){
      return send_error($response, "invalid admin session", 403);
  }
  $deletion_progress = User::delete_multiple_students($users);
  return $response->withJSON($deletion_progress);
});

这是方法错误:

public static function delete_multiple_students($users){
    $invalid_users = array("invalid_email" => array(), "invalid_username" => array());
    $usernames = explode(",", $users);

    foreach($usernames as $username){
        if (strpos($username, '@') !== false) {
            if(!preg_match("/@email.address$/", $username)){
                array_push($invalid_users["invalid_email"], $username);
            }else{
                $name = preg_replace("/@.+/", "", $username);
                $user_id = self::get_by_username($name)->user_id; // error is right here when first in the list is a email
                if (!$user_id){
                    array_push($invalid_users["invalid_email"], $username);
                }
                self::delete_student($user_id);
            }
        }else{
            $user_id = self::get_by_username($username)->user_id; // error is right here when first in the list is a username
            if (!$user_id){
                array_push($invalid_users["invalid_username"], $username);
            }
            self::delete_student($user_id);
        }
    }
    return $invalid_users;
}

这是get_by_username()以供参考:

public static function get_by_username($username){
    global $database;
    $statement = $database->prepare("SELECT * FROM users WHERE username = ?");
    $statement->execute(array($username));
    $row = $statement->fetch(PDO::FETCH_ASSOC);
    $statement->closeCursor();
    if($row){
        $user = new User($row);
        return $user;
    } else {
        return null;
    }
}

1 个答案:

答案 0 :(得分:0)

经过进一步的广泛测试后,事实证明,尽管后端php代码中出现错误,但真正的问题是AngularJS没有通过快捷方式发送删除请求体。 向任何花费我很多时间研究php以查找此代码中的错误的人致歉。