为什么不能对数组使用execute()?

时间:2019-03-20 17:38:21

标签: php arrays execute

我正在尝试对我的网站实施验证电子邮件流程,但出现错误

  

致命错误:未捕获错误:调用成员函数execute()   bool in:56堆栈跟踪:#0 {main}进入第56行

这是我的代码:

      $user_activation_code = md5(rand());

  // Nuevo Usuario
  $hashed = password_hash($password,PASSWORD_DEFAULT);
  $insert_query = 'INSERT INTO clientes (full_name,email,user_activation_code,user_email_status,password,permissions,street,numero,interior,city,state,zip_code,country) VALUES ("$name","$email","$user_activation_code",:user_email_status,"$hashed",cliente,"$street","$numero","$interior","$city","$state","$zip_code",Mexico)';
  $statement = $db->prepare($insert_query);
  $statement -> execute(
    array(
      'full_name'            => $_POST['name'],
      'email'                => $_POST['email'],
      'password'             => $hashed,
      'user_activation_code' => $user_activation_code,
      ':user_email_status'    => 'no verificado'
    )
  );
  $result = $statement->fetchAll();

1 个答案:

答案 0 :(得分:0)

您需要将数组正确绑定到查询。

要使用关联数组的PDO,您需要在查询字符串中具有值,并在键中具有相同的值。

$insert_query = 'INSERT INTO clientes (full_name,email,user_activation_code,user_email_status,password,permissions,street,numero,interior,city,state,zip_code,country) VALUES (:full_name,:email,:user_activation_code,:user_email_status,:password,"cliente","$street","$numero","$interior","$city","$state","$zip_code","Mexico")';
array(
  ':full_name'            => $_POST['name'],
  ':email'                => $_POST['email'],
  ':password'             => $hashed,
  ':user_activation_code' => $user_activation_code,
  ':user_email_status'    => 'no verificado'
)

还请绑定其他值,例如:$ city,$ state等。