PHP无法创建用户

时间:2019-05-03 22:31:52

标签: php rest api raspberry-pi

我是刚开始在Raspberry上创建REST API。我在其中安装了MySql,并且想要创建注册api。我在网上找到了一个教程,并按照它进行了学习。通过使用编写的API在表中创建用户,我遇到了一些问题。这是我的user_usr表的结构:

enter image description here

我创建了一个PHP类来连接到数据库,如下所示:

<?php
class Database {
    private $host = "localhost";
    private $db_name = "my_db_name";
    private $username = "root";
    private $password = "my_password";
    public $conn;
    function getConnection() {
        $this->conn = null;
        try {
            $this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
        } catch(PDOException $exception) {
            echo "Connection error: " . $exception->getMessage();
        }
        return $this->conn;
    }
}
?>

然后我像这样创建user.php类:

<?php
class User {
    private $conn;
    private $table_name = "user_usr";
    public $id_usr;
    public $name_usr;
    public $surname_usr;
    public $email_usr;
    public $password_usr;
    public function __construct($db){
        $this->conn = $db;
    }
    function create() {
        $query =  "INSERT INTO " . $this->table_name . "
            SET
                name_usr = :name,
                surname_usr = :surname,
                email_usr = :email,
                password_usr = :password";
        $stmt = $this->conn->prepare($query);
        $this->name_usr=htmlspecialchars(strip_tags($this->name_usr));
        $this->surname_usr=htmlspecialchars(strip_tags($this->surname_usr));
        $this->email_usr=htmlspecialchars(strip_tags($this->email_usr));
        $this->password_usr=htmlspecialchars(strip_tags($this->password_usr));
        $stmt->bindParam(':name', $this->name_usr);
        $stmt->bindParam(':surname', $this->surname_usr);
        $stmt->bindParam(':email', $this->email_usr);
        $password_hash = password_hash($this->password_usr, PASSWORD_BCRYPT);
        $stmt->bindParam(':password', $password_hash);
        if($stmt->execute()){
            return true;
        }
        return false;
    }
}
?>

然后我创建类来创建用户(create_user.php),如下所示:

<?php
header("Access-Control-Allow-Origin: http://localhost/api3/");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");

include_once 'config/database.php';
include_once 'objects/user.php';

$database = new Database();
$db = $database->getConnection();

$user = new User($db);

$data = json_decode(file_get_contents("php://input"));

$user->name_usr = $data->name;
$user->surname_usr = $data->surname;
$user->email_usr = $data->email;
$user->password_usr = $data->password;

if ($user->create()) {
    http_response_code(200);
    echo json_encode(array("message" => "User was created."));
} else {
    http_response_code(400);
    echo json_encode(array("message" => "Unable to create user."));
}
?>

当我尝试在以下地址与Postman一起运行API时:http://my_raspberry_ip/api/create_user.php,我发现查询失败了,我也不知道为什么。在邮递员中,出现错误400:

enter image description here

文件$stmt->execute()中的行user.php似乎由于某种原因而失败,并且返回false,因此服务器向我显示了400错误。 有人知道为什么它会失败吗?谁能帮助我找到解决此问题的方法? 谢谢!

0 个答案:

没有答案