drll42469 2017-03-02 14:06
浏览 54

为什么用户不添加到数据库? (PHP,PDO,OOP)

I am new to PHP OOP, so I have problem here. I've searched a lot, but couldn't find any useful information. I am trying to add the users to the database and my code doesn't adding any users to it and it doesn't print any error result. Could someone help me to solve this problem? Where am I doing a mistake? Thank you for any help!

Here is my database.php file:

class Database
{
    private $host = 'DB_HOST';
    private $user = 'DB_USER';
    private $password = 'DB_PASSWORD';
    private $dbName = 'DB_NAME';
    private $dbh;
    private $error;
    private $stmt;
    private $name;
    private $lastname;
    private $employmentDate;

    public function __construct()
    {
        // Setting Database Source Name (DSN)
        $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbName;
        // Setting options
        $options = array(
            PDO::ATTR_PERSISTENT => TRUE,
            PDO::ATTR_ERRMODE    => PDO::ERRMODE_EXCEPTION
        );
        // Making the connection to the database
        try {
            $this->dbh = new PDO($dsn, $this->user, $this->password, $options);
        } catch(PDOException $e) {
            $this->error = $e->getMessage();
        }
    }

    // Avoiding SQL injection with prepare statement
    public function query($query)
    {
        $this->stmt = $this->dbh->prepare($query);
    }

    // Binding the values
    public function bind($param, $value, $type = NULL)
    {
        if(is_null($type)) {
            switch(TRUE) {
                case is_int($value):
                    $type = PDO::PARAM_INT;
                    break;
                case is_bool($value):
                    $type = PDO::PARAM_BOOL;
                    break;
                case is_null($value):
                    $type = PDO::PARAM_NULL;
                    break;
                default:
                    $type = PDO::PARAM_STR;
            }
        }
        $this->stmt->bindValue($param, $value, $type);
    }

    // Executing the prepared statement
    public function execute()
    {
        return $this->stmt->execute();
    }

    public function insertUserValues()
    {
        $database->query('INSERT INTO employee (name,surname,employment_date)
         VALUES (:name, :surname, :employmentDate)');
    }

    public function bindingTheValues()
    {
        $database->bind(':name', $this->name = $_POST['name']);
        $database->bind(':lastname', $this->lastname = $_POST['lastname']);
        $database->bind(':employmentDate', $this->employmentDate = $_POST['employmentDate']);
        $database->execute();
    }
}

// Instantiate database
$database = new Database();
?>

I know that my code is quite long, but I am just trying to insert all the stuff that I've been written.

And here is my entryform.php file:

<?php
// Include database class
include 'database.php';
define("DB_HOST", "localhost");
define("DB_USER", "root");
define("DB_PASSWORD", "");
define("DB_NAME", "employees");
?>

Thank you for any help...

  • 写回答

3条回答 默认 最新

  • douren1891 2017-03-02 14:25
    关注

    aynber is right. You only initialize the Database instance. It will only call __construct method.

    You need to call methods you want to execute after it. For example:

    $database = new Database();
    $database->insertUserValues();
    

    However, this will only give you errors, which you can debug on you own way.

    Or you can try following which should work:

    <?php
    class Database
    {
       private $host = 'DB_HOST';
        private $user = 'DB_USER';
        private $password = 'DB_PASSWORD';
        private $dbName = 'DB_NAME';
        private $dbh;
        private $error;
        private $stmt;
        private $name;
        private $lastname;
        private $employmentDate;
    
        public function __construct()
        {
            // Setting Database Source Name (DSN)
            $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbName;
            // Setting options
            $options = array(
                PDO::ATTR_PERSISTENT => TRUE,
                PDO::ATTR_ERRMODE    => PDO::ERRMODE_EXCEPTION
            );
            // Making the connection to the database
            try {
                $this->dbh = new PDO($dsn, $this->user, $this->password, $options);
            } catch(PDOException $e) {
                $this->error = $e->getMessage();
            }
        }
    
        // Avoiding SQL injection with prepare statement
        public function query($query)
        {
            $this->stmt = $this->dbh->prepare($query);
        }
    
        // Binding the values
        public function bind($param, $value, $type = NULL)
        {
            if(is_null($type)) {
                switch(TRUE) {
                    case is_int($value):
                        $type = PDO::PARAM_INT;
                        break;
                    case is_bool($value):
                        $type = PDO::PARAM_BOOL;
                        break;
                    case is_null($value):
                        $type = PDO::PARAM_NULL;
                        break;
                    default:
                        $type = PDO::PARAM_STR;
                }
            }
            $this->stmt->bindValue($param, $value, $type);
        }
    
        // Executing the prepared statement
        public function execute()
        {
            return $this->stmt->execute();
        }
    }
    
    
    $database = new Database();
    // prep
    $database->query('INSERT INTO employee (name,surname,employment_date)
     VALUES (:name, :surname, :employmentDate)');
    
     // bind
    $database->bind(':name', $_POST['name']);
    $database->bind(':lastname', $_POST['lastname']);
    $database->bind(':employmentDate', $_POST['employmentDate']);
    
    // execute
    $database->execute();
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大