Php 更新SQL语句在外部类中不起作用

Php 更新SQL语句在外部类中不起作用,php,mysql,xampp,Php,Mysql,Xampp,我正在尝试更新MySQL数据库表。 我发现当我在里面插入UPDATESQL语句时 外部类在执行SQL时出现以下错误: 拒绝用户“root”@“localhost”的访问(使用密码:是) 但是,如果我在接收到的文件中执行SQL语句 我想用POST更新的值,我没有得到那个错误。尽管 我在两个上下文中使用相同的连接类 我使用localhost作为服务器,并使用用户名“root”登录。 我没有密码,只传递一个空字符串作为密码。在所有其他上下文中,这不会产生任何问题,并且SQL工作正常 我尝试将密码与用户

我正在尝试更新MySQL数据库表。 我发现当我在里面插入UPDATESQL语句时 外部类在执行SQL时出现以下错误: 拒绝用户“root”@“localhost”的访问(使用密码:是)

但是,如果我在接收到的文件中执行SQL语句 我想用POST更新的值,我没有得到那个错误。尽管 我在两个上下文中使用相同的连接类

我使用localhost作为服务器,并使用用户名“root”登录。 我没有密码,只传递一个空字符串作为密码。在所有其他上下文中,这不会产生任何问题,并且SQL工作正常

我尝试将密码与用户关联,并在config.inc.php文件中对其进行更改,以确保一切都按其应有的方式设置。但即使有密码,它仍然会出现错误声明,说它拒绝使用密码“是”进行访问。这很奇怪,因为我没有要求它使用那个密码

不起作用的程序: 更新类:

class DataUpdaters extends DataConnector {
    public $BrugerID = "Unset";
    public $Password = "Unset";
    public $Email = "Unset";
    public $Fornavn = "Unset";
    public $Efternavn = "Unset";
    public $Telefon = "Unset";
    public $BeskedID = "Unset";
    public $Laest = "Unset";

    function UpdateUser() {
        $Con = $this->CreateCon();
        $UpdateBruger = "UPDATE brugere SET Password='" . $this->Password. "', Email='" . $this->Email . "' WHERE Id=" . $this->BrugerID . " ;";
        mysqli_query($Con, $UpdateBruger);
        mysqli_close($Con);
    }

    function UpdateProfile() {
        $Con = $this->CreateCon();
        $UpdateProfile = "UPDATE profiler SET Fornavn ='" .$this->Fornavn . "', Efternavn='" . $this->Efternavn. "', Telefon='" . $this->Telefon . "' WHERE BrugerID=" . $this->BrugerID . ";";
        mysqli_query($Con, $UpdateProfile);
        mysqli_close($Con);
    }
它扩展的DataConnector:

class DataConnector {
    //Connection Properties
    public $ServerName = "localhost";
    public $UserName = "root";
    public $Password = "";
    public $DBName = "alpacadb";

    function CreateCon() {
        $ReturnCon = mysqli_connect($this->ServerName, $this->UserName, $this->Password, $this->DBName);
        return $ReturnCon;
    }
}
如何在接收信息的php文件中传递信息:

            $Password = $_POST['Password'];
            $Email = $_POST['Email'];
            $Fornavn = $_POST['Fornavn'];
            $Efternavn = $_POST['Efternavn'];
            $Telefon = $_POST['Telefon'];

            $Updaater = new DataUpdaters();
            $Updaater->Password = $Password;
            $Updaater->Email = $Email;
            $Updaater->BrugerID = $BrugerID;
            $Updaater->UpdateUser();

            $Updaater->Fornavn = $Fornavn;
            $Updaater->Efternavn = $Efternavn;
            $Updaater->Telefon = $Telefon;
            $Updaater->UpdateProfile();
有效的程序:

           $Connector = new DataConnector();
           $Con = $Connector->CreateCon();
            $Password = $_POST['Password'];
            $Email = $_POST['Email'];
            $Fornavn = $_POST['Fornavn'];
            $Efternavn = $_POST['Efternavn'];
            $Telefon = $_POST['Telefon'];

            $UpdateUser = "UPDATE brugere SET Password='" . $Password. "', Email='" . $Email . "' WHERE Id=" . $BrugerID. " ;";
            mysqli_query($Con, $UpdateUser);

            $UpdateProfile = "UPDATE profiler SET Fornavn ='" .$Fornavn . "', Efternavn='" . $Efternavn . "', Telefon='" . $Telefon . "' WHERE BrugerID=" . $BrugerID . ";";
            mysqli_query($Con, $UpdateProfile);
            mysqli_close($Con);
我认为SQL语句是否在类中并不重要, 但是当我把它放在一个类中时,我得到了错误消息 “拒绝用户‘root’@‘localhost’(使用密码:YES)的访问。”。
即使用户、服务器和数据库信息在更新我的数据库表的两种方式中是相同的。

您在这两个类中都分配了$Password

public $Password = "Unset";

在DataUpdaters中,重写基类的空密码。在其中一个类中使用不同的变量名来解决连接问题。

感谢您的建议,我将对此进行研究。这只是一个培训PHP和编程的项目,所以安全性不是我在这个州的首要任务。但总有一天会的。你正在学习非常坏的习惯,这些习惯会回来找你。寻找更好的学习资源。到目前为止,我已经在sololearn.com上学习了PHP课程,并且经常使用w3schools.com。你有什么建议吗?有。停止使用学校。它以非常糟糕的例子而闻名。看看Well上那些看起来很有趣的文章,我会研究一下。谢谢