【问题标题】:Insert statement not working using execute(array()) of PDO Extension使用 PDO 扩展的 execute(array()) 插入语句不起作用
【发布时间】:2013-03-09 22:31:45
【问题描述】:
 $stmt = $conn->prepare("INSERT INTO user VALUES ('',:username,md5(:password),'',1,'','',:email,'',0,0,'',:cover,:dateofbirthYear:dateofbirthMonth:dateofbirthDay,NOW(),:sex,:country)");
 $stmt->execute(array(
  ':username'   => $username,
  ':password' => $password,
  ':email'   => $email,
  ':cover' => $cover,
  ':dateofbirthYear'   => $dateofbirthYear,
  ':dateofbirthMonth' => $dateofbirthMonth,
  ':dateofbirthDay'   => $dateofbirthDay,
  ':sex' => $sex,
  ':country'   => $country 
    ));

由于某种原因,此插入语句不起作用。我是 PDO 的新手,所以我不太了解它。我做错了什么?

这个语句给了我这个错误:

致命错误:未捕获的异常“PDOException”和消息“SQLSTATE [HY093]:无效的参数号:绑定变量的数量与/home/manga/public_html/new/register.php:80 中的令牌数量不匹配”堆栈跟踪:
#0 /home/manga/public_html/new/register.php(80): PDOStatement->execute(Array)
#1 {main} 在第 80 行的 /home/manga/public_html/new/register.php 中抛出

【问题讨论】:

  • “我已经在 stackoverflow 上查找了错误”您是否尝试过自己考虑一下?
  • @feeela 如果我没有,我为什么要把问题放在这里?
  • 你真的应该 not be using md5hash passwords

标签: pdo php


【解决方案1】:

您以错误的方式准备查询

INSERT INTO user VALUES ('',:username,md5(:password),'',1,'','',:email,'',0,0,'',
:cover,:dateofbirthYear:dateofbirthMonth:dateofbirthDay,NOW(),:sex,:country
     // ^ These need to either single or separated

对于你正在尝试的,你可以这样做

//Prepare the date of birth earlier
$dob = $dateofbirthYear.$dateofbirthMonth.$dateofbirthDay;

//Then pass it as a single $variable

$stmt = $conn->prepare("INSERT INTO user VALUES ('',:username,md5(:password),'',1,'','',:email,'',0,0,'',:cover,:dob,NOW(),:sex,:country)");
 $stmt->execute(array(
  ':username'   => $username,
  ':password' => $password,
  ':email'   => $email,
  ':cover' => $cover,
  ':dob'   => $dob, // <-- Problem solved
  ':sex' => $sex,
  ':country'   => $country 
    ));
 // Then it will execute

【讨论】:

  • 我不敢相信这是错误!感谢您的帮助。
【解决方案2】:

您收到的确切错误消息是:

SQLSTATE[HY093]: 参数号无效:绑定变量的数量与标记的数量不匹配

这意味着您传递的参数数量/名称(execute 中的array())与您在prepare() SQL 查询中的参数数量/名称不匹配。

如果您compare that with the other questions that contain SQLSTATE[HY093],您会发现它通常与较大且格式错误且难以阅读的代码有关。这让人很难计算。然后你对某事有疏忽,然后错误就发生了。

只要修好就行了,比如你不能用三个名字来做一个参数:

,:dateofbirthYear:dateofbirthMonth:dateofbirthDay,

而是只为生日传递一个参数:

, :dateofbirth, 

您还可以使您的代码更具可读性:

$stmt = $conn->prepare(
    "INSERT INTO user
     VALUES (
        '', :username, md5(:password), '', 1, '', '', :email, '', 0, 0, '',
        :cover, :dateofbirth, NOW(), :sex, :country
     )"
);
$stmt->execute(array(
    ':username'    => $username,
    ':password'    => $password,
    ':email'       => $email,
    ':cover'       => $cover,
    ':dateofbirth' => $dateofbirthYear . $dateofbirthMonth . $dateofbirthDay,
    ':sex'         => $sex,
    ':country'     => $country
));

然后你有一个密码哈希的安全问题:

md5(:password)

改为进行正确的密码散列,请参阅PHP FAQ about Safe Password Hashing

【讨论】:

  • 我很抱歉,但我不喜欢你刚刚让我的答案因该编辑而过时:(
  • 我怎样才能让你再次微笑?
  • 这是最意想不到的反应。 :)(撤回我的投票)
  • 您的答案类似于@Starx 的答案。我选择了他作为正确答案,因为他首先回答了它。但谢谢你的澄清。我会看看那个密码哈希的东西。
  • 是的,请检查密码哈希,常见问题解答解释了md5() 的问题。我们在网站上也有一些很好的问答,这些问答将进入更多细节。
【解决方案3】:

更正准备好的查询:

$stmt = $conn->prepare("INSERT INTO user VALUES ('',:username,md5(:password),'',1,'','',:email,'',0,0,'',:cover,:dateofbirthYear,:dateofbirthMonth:,dateofbirthDay,NOW(),:sex,:country)");
//:dateofbirthYear,:dateofbirthMonth:,dateofbirthDay place holders are seprated 

$stmt->execute(array(
 ':username'   => $username,
  ':password' => $password,
  ':email'   => $email,
  ':cover' => $cover,
  ':dateofbirthYear'   => $dateofbirthYear,
  ':dateofbirthMonth' => $dateofbirthMonth,
  ':dateofbirthDay'   => $dateofbirthDay,
  ':sex' => $sex,
  ':country'   => $country 
));

【讨论】:

    猜你喜欢
    • 2015-02-22
    • 1970-01-01
    • 2012-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-26
    • 2013-05-19
    相关资源
    最近更新 更多