【发布时间】:2018-10-20 03:33:05
【问题描述】:
你能帮帮我吗?我的脚本有点问题。
database.php
class Database {
protected $host = "localhost";
protected $dbname = "phppdo";
protected $user = "root";
protected $pass = "";
protected $DBH;
public function __construct() {
try {
$this->DBH = new PDO("mysql:host=$this->host;dbname=$this->dbname", $this->user, $this->pass);
}
catch (PDOException $e) {
die($e->getMessage());
}
return $this->DBH;
}
}
user.php
date_default_timezone_set('America/Sao_Paulo');
include 'database.php';
class User extends Database {
private $name;
private $email;
private $date;
function __construct($name, $email) {
$this->$name = $name;
$this->$email = $email;
}
public function insert() {
$STH = $this->DBH->prepare("INSERT INTO `phppdo` VALUES(NULL, :username, :email, UNIX_TIMESTAMP())");
$STH->execute(array(
':name' => $name,
':emal' => $email,
));
}
}
if(isset($_POST['submit'])) {
$x = new User($_POST['name'], $_POST['email']);
$x->insert();
}
脚本不工作!我得到Call to a member function prepare() on a non-object on line 16 = $STH = $this->DBH->prepare("INSERT INTOphppdoVALUES(NULL, :username, :email, UNIX_TIMESTAMP())");
我不知道为什么会这样……你能帮帮我吗?谢谢!!
【问题讨论】: