执行查询时出错
好吧,我正在为我的网站创建一个注册系统,但我在执行查询时遇到问题。我尝试解决该问题,但没有成功。有点困惑:(
这是我的代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您已将 PDO::prepare() 与 < a href="http://www.php.net/manual/en/pdo.query.php" rel="nofollow">PDO::query():
它应该是:
或者:
来自文档:
如果您要重复发出相同的语句,或者如果您需要绑定参数。据我所知,在使用 PDO::query()。
请注意,使用 PDO::prepare() 您可以使用 PDOStatement::bindParam() 在调用 PDOStatement->execute(),或者可以将参数作为数组传递到 PDOStatement->execute()。
您还需要在数组键前添加冒号作为前缀。所以最终结果将是:
I think you have got PDO::prepare() mixed up with PDO::query():
It should be either:
Or:
From the docs:
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:
您应该在插入查询中使用 ` 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)query()函数执行sql语句。你应该使用prepare()函数。
由于 pdo 标签,我假设您正在使用 pdo
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