简体   繁体   English

PDO更新数据库

[英]PDO update database

Hi I'm new to PHP and PDO. 嗨,我是PHP和PDO的新手。 I am trying to update a record in my database depending on the login_id variable that I have declared in my sessions file (my session are working fine). 我试图根据我在会话文件中声明的login_id变量更新数据库中的记录(我的会话运行正常)。

I have the following code for the update script. 我有以下更新脚本代码。 It is throwing up an errors with the execute statement. 它在execute语句中引发错误。

"Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens' in /home/atlismap/public_html/bio.php:40 Stack trace: #0 /home/atlismap/public_html/bio.php(40): PDOStatement->execute() #1 {main}" /home/atlismap/public_html/bio.php:40中的“未捕获的异常'PDOException',消息为'SQLSTATE [HY093]:无效的参数号:绑定变量的数量与令牌的数量不匹配”:40堆栈跟踪:#0 / home / atlismap / public_html / bio.php(40):PDOStatement-> execute()#1 {main}“

Does anybody know why? 有人知道为什么吗?

require_once 'check.php';
$sql = "UPDATE users SET username = :username, 
        full_name = :full_name, 
        country = :country,   
        bio = :bio  
        WHERE id = : $log_user_id";
$stmt = $dtb->prepare($sql);                                  
$stmt->bindParam(':full_name', $_POST['full_name'], PDO::PARAM_STR);          
$stmt->bindParam(':country', $_POST['country'], PDO::PARAM_STR);
$stmt->bindParam(':bio', $_POST['bio'], PDO::PARAM_STR);   
$stmt->execute(); 

You have to set ':username' like other params: 您必须像其他参数一样设置':username':

$stmt->bindParam(':username', $_POST['username'], PDO::PARAM_STR); 

And fix: 并修复:

$sql = "UPDATE users SET
…
WHERE id = : $log_user_id";

by: 通过:

$sql = "UPDATE users SET 
…
WHERE id = :id";
$stmt->bindParam(':id', $log_user_id, PDO::PARAM_INT); 

PDO permits to bind param with ":param_name" but you use a variable for you id so you can use the param ":id" and bind it, or use "WHERE id=$variable. PDO允许使用“:param_name”绑定参数,但是您为id使用变量,因此可以使用参数“:id”进行绑定,或者使用“ WHERE id = $ variable”。

There are two problems I see. 我看到两个问题。

You don't have a variable binded to :username and there is a problem that lies at WHERE id = : $log_user_id"; 您没有将变量绑定到:username,并且存在WHERE id = : $log_user_id";

You should either remove the : and just use '$log_user_id' (if this is safe) or use :log_user_id and bind it below. 您应该删除:并仅使用'$log_user_id' (如果安全)或使用:log_user_id并将其绑定在下面。

Another way to do this is just to bind them in execute which I just found easy and less code to write: 做到这一点的另一种方法是将它们绑定到execute中,我发现它编写起来简单易行,代码更少:

$sql = "UPDATE users SET username = ?, 
        full_name = ?, 
        country = ?,   
        bio = ?  
        WHERE id = ?";
$stmt = $dtb->prepare($sql);                                   
$stmt->execute(array($_POST['username'], $_POST['full_name'], $_POST['country'], $_POST['bio'], $log_user_id));

Just bind them in the order in which you put the ?'s in the query 只需按照将?放入查询的顺序将它们绑定

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM