没有任何结果!!pdo代码是否有错误?


not getting any results!! is there an error with the pdo code?

代码没有给出任何结果,你能帮我吗?

$f_name = $_POST['first_name'];
$l_name = $_POST['last_name'];
$e_mail = $_POST['email']; 
$sql = "INSERT INTO
            informations(first name, last name,email ,email)
        VALUES($f_name,$l_name,$e_mail)";
 /*$result = pdo::query($sql);
if(!$result)*/
    $result = $sql->fetch(PDO::FETCH_ASSOC);
if(!$result)
{
    //something went wrong, display the error
    echo 'Something went wrong while registering. Please try again later.';
    //echo mysql_error(); //debugging purposes, uncomment when needed
}
else
{
    echo 'Successfully registered. You can now <a href="signin.php">sign in</a> and start posting! :-)';
}

您当前没有正确使用PDO,您正在通过插入查询获取结果,并且您没有充分利用参数绑定。

试试这个:

// make sure $db is initialized as a new PDO object
// use :param placeholders for binding
$sql = "INSERT INTO informations(first name, last name, email) VALUES(:first, :last, :email)";
// prepare query
$query = $db->prepare($sql);
// set up parameter binding replacements    
$binding = array(
    ':first' => $f_name,
    ':last' => $l_name,
    ':email' => $e_mail // strange variable name splitting...
);
// execute the query (returns boolean true or false)
$query_results = $query->execute($binding);
// process result message
if($query_results) {
    echo 'Success';
} else {
    echo 'Something went wrong! Error: ' . $query->errorInfo();
}
参考:

  • PDO: execute()手册
  • PDO入门教程
  • 另一个PDO教程