无法使用扩展的PHP类将新用户添加到数据库中

时间:2016-05-05 06:23:52

标签: php oop

我还在学习关于PHP OOP的在线课程我有一个关于继承的练习,想要将连接类扩展到新类中以向db添加新数据。

这是connection.php类:

<?php
class connectionClass
{
    private $db_host, $db_name, $db_user;
    private $db_pass;
    private $connection;

    function __construct($host = "localhost", $name="userDB", $user = "root", $pass="igts")
    {
        $this->db_host = $host;
        $this->db_name = $name;
        $this->db_user = $user;
        $this->db_pass = $pass;
    }

    public function connect()
    {
        $this->connection = new PDO("mysql:host={$this->db_host};dbname={$this->db_name}", $this->db_user, $this->db_pass);
        $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        return $this->connection;
    }
}
?>

这是新课程:

<?php
include('connection.php');
class insertUser extends connectionClass
{
    public $msg;
    private $connection;
    function __construct()
    {
        $this->connection = $this->connect();
    }

    public function insert_user($user, $pass, $date)
    {

            $sql = "INSERT INTO login(user_name, user_pass, date_in) VALUES(:user, :pass, :date_in)";
            $insert = $this->connection->prepare($sql);
            $insert->bindValue(':user', $user);
            $insert->bindValue(':pass', $hashed);
            $insert->bindValue(':date_in', $date);
            $insert->execute();
            header("Location: index.php");
        }

}

?>

我在添加用户时得到了:

  

未捕获的异常&#39; PDOException&#39;消息&#39; SQLSTATE [3D000]:   无效的目录名称:1046未选择数据库&#39;

0 个答案:

没有答案