使用参数使用PDO更新记录

时间:2012-01-28 10:20:53

标签: php mysql parameters pdo

我正在尝试使用PDO更新我的数据库表,但为了避免安全漏洞,我在查询中使用了参数。

我收到了这个错误:
exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number: parameter was not defined' in D:\xampp\htdocs\logansarchive\admin\do.article.php:25 Stack trace: #0 D:\xampp\htdocs\logansarchive\admin\do.article.php(25): PDOStatement->execute() #1 {main}

这是我的代码:

$sql = "update articles set".
    "category = :cat,".
    "articletitle = :title,".
    "articlecontent = :content".
    "where articleid = :target";

$result = $dbh->prepare($sql);
$result->bindParam(":cat", $category);
$result->bindParam(":title", $title);
$result->bindParam(":content", $content);
$result->bindParam(":target", $target);
$result->execute();

我找到了Not able to update rows using PDO 看起来和我正在做的很相似,但是我不知道如何根据这个来修复我的代码,因为我看不出回答这个问题的人所列出的任何问题。

非常感谢任何帮助。

谢谢你的时间!

2 个答案:

答案 0 :(得分:0)

$sql = "update articles set ". // whitespace after set
"category = :cat,".
"articletitle = :title,".
"articlecontent = :content ". // whitespace after :content
"where articleid = :target";

$result = $dbh->prepare($sql);
$result->bindParam(":cat", $category, PDO::PARAM_INT);
$result->bindParam(":title", $title, PDO::PARAM_STR);
$result->bindParam(":content", $content, PDO::PARAM_STR);
$result->bindParam(":target", $target, PDO::PARAM_INT);
$result->execute();

答案 1 :(得分:0)

我们的两个代码都是正确的,但sql语句有两个小错误。如果你回应你的sql然后你可以看到你忘记了两个空格...在设置“更新文章集”之后排在第一行,第二行在第4行之后:内容“articlecontent =:content”...现在它应该工作!

相关问题