php pdo,我创建了一个使用魔术函数进行连接的类和一个用于插入数据的函数,但是它不起作用

时间:2019-07-09 01:56:44

标签: php mysql pdo

好的,我对php很陌生,如果这很简单,请原谅。我有三个文件。

一个HTML文件,该文件的格式具有用户电子邮件和密码输入以及一个提交按钮。

然后我有一个名为db的类,该类使用构造魔术函数来调用pdo connect方法。该类还具有一个公共函数,该函数带有三个参数,即用户电子邮件和传递参数,并通过准备好的语句运行它们以将其插入数据库。

第三个文件是一个php文件,其中包括前面提到的类,它实例化(不确定那是否是正确的词)应创建连接的类。然后我检查是否设置了提交按钮,如果已设置,它将用户输入传递给我创建的插入方法。

问题。当我点击“提交”按钮时,屏幕上将显示以下内容

insert("$user","$email","$pass"); } ?>

没有错误,并且数据没有出现在数据库中。这是代码

HTML

<html>
    <head>
        <title>Register now</title>
        <link rel="stylesheet" href="styles/reg_s.css" >
    </head>

    <body>
        <div id="center-box">
            <form method="post" action="register.php">
                <input type="text" name="username" placeholder="Enter a username">
                <input type="email" name="email" placeholder="Enter your email address">
                <input type="password" name="password" placeholder="Enter a password">
                <input type="submit" name="submit" value="submit">
            </form>
        </div>
    </body>
</html>

classes.php文件

<?php 

class db {

    public $host = "localhost";
    public $db_name = "cms_project";
    public $user = "root";
    public $pass = "1234";

    public $link;

    private function connect(){
        $this->link = new pdo("mysql:host=$this->host;dbname=$this->db_name",$this->user,$this->pass);
    }

    public function __construct(){
        $this->connect();
    }

    public function insert($user,$email,$pass){

        $sql = "INSERT INTO user(username,email,password) VALUES(:1,:2,:3)";

        $stmt = $this->link->prepare($sql);
        $stmt->bindvalue(":1",$user);
        $stmt->bindvalue(":2",$email);
        $stmt->bindvalue(":3",$pass);

        $stmt->execute();
    }
}

?>

和表格处理页面

<?php 

include "php_includes/classes.php";

$db_handle = new db();

if (isset($_POST["submit"])){
    $user = $_POST["username"];
    $email = $_POST["email"];
    $pass = $_POST["password"];

    $db_handle->insert("$user","$email","$pass");
}

?>

以下是我收到的错误消息

Fatal error: Uncaught PDOException: PDO::__construct(): The server requested authentication method unknown to the client [caching_sha2_password] in C:\wamp64\www\cms\php_includes\classes.php:15 Stack trace: #0 C:\wamp64\www\cms\php_includes\classes.php(15): PDO->__construct('mysql:host=loca...', 'root', 'Cp22121988') #1 C:\wamp64\www\cms\php_includes\classes.php(19): db->connect() #2 C:\wamp64\www\cms\register.php(5): db->__construct() #3 {main} Next PDOException: SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client in C:\wamp64\www\cms\php_includes\classes.php on line 15

PDOException: PDO::__construct(): The server requested authentication method unknown to the client [caching_sha2_password] in C:\wamp64\www\cms\php_includes\classes.php on line 15

2 个答案:

答案 0 :(得分:0)

在您的PHP代码文件的顶部,您具有:    <? php

应该是    <?php (没有空间)。

当我不小心进行正则表达式查找/替换并添加空格时,发生了同样的事情,这使得服务器无法运行php代码

答案 1 :(得分:-3)

this StackOverflow question,执行以下SQL:

SET PASSWORD FOR 'username'@'localhost' = PASSWORD('enter password here')

这应该解决您收到的错误。但是一旦解决,您对insert()函数的调用就会遇到另一个问题:

$db_handle->insert("$user","$email","$pass");

除非您特别想在数据库中输入文字文本“ $ user”,“ $ email”和“ $ pass”,否则不要在这些变量周围加上引号。应该是:

$db_handle->insert($user, $email, $pass);