【问题标题】:Update SQL statements not working in external class更新在外部类中不起作用的 SQL 语句
【发布时间】:2019-09-04 13:02:47
【问题描述】:

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

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

我使用 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 语句是否在一个类中并不重要, 但是当我把它放在一个类中时,我得到了错误消息 “用户'root'@'localhost'的访问被拒绝(使用密码:YES)”。 即使在更新我的数据库表的两种方式中,用户、服务器和数据库信息是相同的。

【问题讨论】:

  • 感谢您的建议,我会调查的。这只是一个训练 PHP 和一般编程的项目,所以在这个状态下,安全不是我的首要任务。但在某个时候它会是。
  • 你正在学习非常坏的习惯,这些习惯会卷土重来。寻找更好的学习资源。
  • 到目前为止,我已经在sololearn.com上学习了PHP课程,并且经常使用w3schools.com。你有什么建议吗?
  • 是的。停止使用 w3schools。它以非常糟糕的例子而闻名。查看phpdelusions.net的文章
  • 看起来很有趣,我会研究一下。谢谢。

标签: php mysql xampp


【解决方案1】:

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

public $Password = "Unset";

在 DataUpdaters 中覆盖了基类的空密码。在这些类之一中使用不同的变量名来解决连接问题。

【讨论】:

    猜你喜欢
    • 2015-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多