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

Update SQL statements not working in external class

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

如果我在接收到的文件中执行 SQL 语句 我想使用 POST 更新的值,我没有收到该错误。虽然 我在两种情况下都使用相同的连接 class。

我使用 localhost 作为服务器,并使用用户名 "root" 登录。 我没有密码,只是传递一个空字符串作为密码。在没有任何问题的所有其他情况下,SQL 工作正常。

我已尝试将密码与用户相关联并在 config.inc.php 文件中更改它以确保一切设置正确。但即使有密码,它仍然带有错误声明,说它拒绝使用密码 "YES" 进行访问。这很奇怪,因为我没有要求它使用那个密码。

无效的程序: 更新类:

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 语句是否在 class 中并不重要, 但是当我把它放在 class 中时,我得到了错误消息 "Access denied for user 'root'@'localhost' (using password: YES)"。 即使用户、服务器和数据库信息在更新我的数据库的两种方式中是相同的 table.

您在两个 class 中都分配了 $Password。

public $Password = "Unset";

在 DataUpdaters 中覆盖了基础 class 的空密码。在其中一个 class 中使用不同的变量名来解决连接问题。