执行查询时出错

发布于 2024-11-30 18:02:34 字数 1225 浏览 2 评论 0原文

好吧,我正在为我的网站创建一个注册系统,但我在执行查询时遇到问题。我尝试解决该问题,但没有成功。有点困惑:(

这是我的代码:

public function registerUser($username, $password, $email) {
            global $core;
            if(empty($username) || empty($password) || empty($email)) {
                throw new Exception ('A required field was left blank');
            } else {
                //SQL Query Data
                $data['username'] = $username;
                $data['password'] = $core->encrypt($password);
                $data['email'] = $email;
                $data['userKey'] = rand(999999, 100000);
                $data['ip'] = $_SERVER['REMOTE_ADDR'];
                $data['date'] = time();

                //SQL Query
                $sql = "INSERT INTO `u_userdata` ('user-key', 'username', 'password', 'email', 'register-ip', 'register-date') VALUES (:userKey, :username, :password, :email, :ip, :date)";
                $STH = $this->DBH->query($sql);
                $STH->execute($data);
            }
        }

这是我收到的错误:

Fatal error: Call to a member function execute() on a non-object in C:\xampp\htdocs\community\inc\user.inc.php on line 33

我猜这是查询错误,但我不确定是什么!

Well, I'm creating a registration system for my website but I'm having trouble executing my query. I've tried to troubleshoot the problem, but I've had no success. Kind of confused :(

Here is my code:

public function registerUser($username, $password, $email) {
            global $core;
            if(empty($username) || empty($password) || empty($email)) {
                throw new Exception ('A required field was left blank');
            } else {
                //SQL Query Data
                $data['username'] = $username;
                $data['password'] = $core->encrypt($password);
                $data['email'] = $email;
                $data['userKey'] = rand(999999, 100000);
                $data['ip'] = $_SERVER['REMOTE_ADDR'];
                $data['date'] = time();

                //SQL Query
                $sql = "INSERT INTO `u_userdata` ('user-key', 'username', 'password', 'email', 'register-ip', 'register-date') VALUES (:userKey, :username, :password, :email, :ip, :date)";
                $STH = $this->DBH->query($sql);
                $STH->execute($data);
            }
        }

and here is the error I'm getting:

Fatal error: Call to a member function execute() on a non-object in C:\xampp\htdocs\community\inc\user.inc.php on line 33

I'm guessing it's an error with the query, but I'm not sure what!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏末的微笑 2024-12-07 18:02:34

我认为您已将 PDO::prepare() 与 < a href="http://www.php.net/manual/en/pdo.query.php" rel="nofollow">PDO::query():

它应该是:

$result = $this->DBH->query($sql);

或者:

$STH = $this->DBH->prepare($sql);
$STH->execute($data);

来自文档:

PDO::query() 在 a 中执行 SQL 语句单一函数调用,
将语句返回的结果集(如果有)返回为
PDOStatement 对象。

如果您要重复发出相同的语句,或者如果您需要绑定参数。据我所知,在使用 PDO::query()

请注意,使用 PDO::prepare() 您可以使用 PDOStatement::bindParam() 在调用 PDOStatement->execute(),或者可以将参数作为数组传递到 PDOStatement->execute()

您还需要在数组键前添加冒号作为前缀。所以最终结果将是:

$data[':username'] = $username;
$data[':password'] = $core->encrypt($password);
$data[':email'] = $email;
$data[':userKey'] = rand(999999, 100000);
$data[':ip'] = $_SERVER['REMOTE_ADDR'];
data[':date'] = time();

//SQL Query
$sql = "INSERT INTO `u_userdata` ('user-key', 'username', 'password', 'email', 'register-ip', 'register-date') VALUES (:userKey, :username, :password, :email, :ip, :date)";
$STH = $this->DBH->prepare($sql);
$STH->execute($data);

I think you have got PDO::prepare() mixed up with PDO::query():

It should be either:

$result = $this->DBH->query($sql);

Or:

$STH = $this->DBH->prepare($sql);
$STH->execute($data);

From the docs:

PDO::query() executes an SQL statement in a single function call,
returning the result set (if any) returned by the statement as a
PDOStatement object.

You would normally use PDO::prepare() if you are going to issue the same statement repeatedly, or if you need to bind parameters. As far as I am aware, it is not possible to bind parameters to your query prior to using PDO::query().

Note that with PDO::prepare() you can either use PDOStatement::bindParam() to bind parameters prior to calling PDOStatement->execute(), or you can pass the parameters as an array to PDOStatement->execute().

You also need to prefix your array keys with a colon. So the final result would be:

$data[':username'] = $username;
$data[':password'] = $core->encrypt($password);
$data[':email'] = $email;
$data[':userKey'] = rand(999999, 100000);
$data[':ip'] = $_SERVER['REMOTE_ADDR'];
data[':date'] = time();

//SQL Query
$sql = "INSERT INTO `u_userdata` ('user-key', 'username', 'password', 'email', 'register-ip', 'register-date') VALUES (:userKey, :username, :password, :email, :ip, :date)";
$STH = $this->DBH->prepare($sql);
$STH->execute($data);
平安喜乐 2024-12-07 18:02:34

您应该在插入查询中使用 ` quote 而不是 ' 。

“插入u_userdata用户密钥用户名密码电子邮件注册IP注册日期)值(:userKey,:用户名,:密码,:电子邮件,:ip,:日期)

You should use ` quote instead of ' in insert query.

"INSERT INTO u_userdata (user-key, username, password, email, register-ip, register-date) VALUES (:userKey, :username, :password, :email, :ip, :date)

﹉夏雨初晴づ 2024-12-07 18:02:34

query()函数执行sql语句。你应该使用prepare()函数。
由于 pdo 标签,我假设您正在使用 pdo

    $data[':username'] = $username;
    $data[':password'] = $core->encrypt($password);
    $data[':email'] = $email;
    $data[':userKey'] = rand(999999, 100000);
    $data[':ip'] = $_SERVER['REMOTE_ADDR'];
    $data[':date'] = time();

    //SQL Query
    $sql = "INSERT INTO `u_userdata` ('user-key', 'username', 'password', 'email', 'register-ip', 'register-date') VALUES (:userKey, :username, :password, :email, :ip, :date)";
    $stmt = $this->DBH->prepare($sql);
    $stmt->execute($data);

the query() function executes the sql statement. you should use the prepare() function.
i'm assuming that you are using pdo, because of the pdo tag

    $data[':username'] = $username;
    $data[':password'] = $core->encrypt($password);
    $data[':email'] = $email;
    $data[':userKey'] = rand(999999, 100000);
    $data[':ip'] = $_SERVER['REMOTE_ADDR'];
    $data[':date'] = time();

    //SQL Query
    $sql = "INSERT INTO `u_userdata` ('user-key', 'username', 'password', 'email', 'register-ip', 'register-date') VALUES (:userKey, :username, :password, :email, :ip, :date)";
    $stmt = $this->DBH->prepare($sql);
    $stmt->execute($data);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文