无法使用PDO在表中插入行

时间:2015-04-25 12:36:06

标签: php pdo

我想使用PDO将一些数据插入表中。我找了一些例子,我发现我需要使用函数prepare,bind然后执行,但是我无法弄清楚我的代码是什么,我做错了因为它什么都没插入,我在查询或php中没有错误代码。

    if($_POST){

    $account    = $_POST['account'];
    $password   = $_POST['password'];
    $phone      = $_POST['phone'];
    $email      = $_POST['email'];

    $stmt =     'INSERT INTO employer(account, password, phone, email) VALUES(:account, :password, :phone, :email)';
    $stmt = $conn->prepare($stmt);

    $stmt->bindParam(':account', $account, PDO::PARAM_STR,100);
    $stmt->bindParam(':password',$password, PDO::PARAM_STR,100);
    $stmt->bindParam(':phone', $phone, PDO::PARAM_STR,100);
    $stmt->bindParam(':email', $email, PDO::PARAM_STR,100);

    if ($stmt->execute(array('account'  => $account,
                             'password' => $password,
                             'phone'    => $phone,
                             'email'    =>$email
                             )
                        )
        ){
    echo "success";
    }else{
        echo "error";
    }

}

1 个答案:

答案 0 :(得分:0)

@jeroen检测到错误我绑定了两次。所以我可以绑定“在执行语句之前绑定或者将数组作为参数发送,而不是两者都发送”

$stmt = $pdo->prepare('
INSERT INTO employer
(account, password, phone, mail)
values (:account, :password, :phone, :mail)');

  $stmt->execute( 
            array(':account'    => $account, 
                  ':password'   => md5($password), 
                  ':phone'      => $phone,
                  ':mail'        => $email
                  )
            );

  if ($pdo->lastInsertId())
    return true;
  else
   return false;
相关问题