0

我正在尝试创建一个简单的用户类,但我无法在数据库中获取数据我尝试了很多不同的代码现在这里是用户类

namespace MonetizeMedia;

class User {
private $uid;
private $fields;

public function __construct() {
    $this->uid = null;
    $this->fields = array('username' => '',
                          'password' => '');
}

public function __get($field) {
    if($field == 'uid')
    {
        return $this->uid;
    }
    else
    {
        return $this->fields[$field];
    }
}

public function __set($field, $value) {
    if(array_key_exists($field, $this->fields))
    {
        $this->fields[$field] = $value;
    }
}

public function createUser() {
    try {

    $db = new \MonetizeMedia\Database;
    $bcrypt = new \MonetizeMedia\Bcrypt(15);

    $sql = "INSERT INTO users(username, password) VALUES(:username, :password)";

    $stmt = $db->prepare($sql);

    $stmt->bindParam(":username", $username);
    $stmt->bindParam(":password", $bcrypt->hash($password));

    $stmt->execute();

    return "Registration Successful";

    } catch ( PDOException $e ) {
        return $e->getMessage();
    }

}

这是注册页面

<?php

ob_start();
session_start();

include 'classes/user.class.php';

if(isset($_POST['submitted'])) {

$username = $_POST['username'];
$password = $_POST['password'];

$User->username = $username;
$User->password = $password;

if($User->createUser()) {
    echo "DONE!";
}
else
{
    echo "An error occured while creating your account. Please try later.";
    return;
}

}

?>
 <!DOCTYPE html>
 <html>
 <head>

<meta charset="UTF-8" />

<title>Register</title>

 </head>
 <body>

 <form method="post" action="">
 <ul>
<li>
<label for="usn">Username : </label>
<input type="text" name="username" />
</li>

<li>
<label for="passwd">Password : </label>
<input type="password" name="password" />
</li>

<li class="buttons">
<input type="submit" name="register" value="Register" />
</li>

  </ul>
  </form>

 </body>
 </html>

我正在尝试学习 php 和 pdo,所以我现在不太好

4

0 回答 0