使用PDO扩展的execute(array())插入语句不起作用

syr*_*ull 1 php pdo

 $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 
    ));
Run Code Online (Sandbox Code Playgroud)

由于某种原因,此insert语句不起作用.我是PDO的新手,所以我对它不太了解.我究竟做错了什么?

这个陈述给了我这个错误:

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

Sta*_*arx 6

您以错误的方式准备了查询

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
Run Code Online (Sandbox Code Playgroud)

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

//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
Run Code Online (Sandbox Code Playgroud)