PHP PDO连接失败

时间:2011-03-25 00:46:06

标签: php mysql insert pdo

嘿,所以我是PDO的新手,我无法弄清楚为什么我的数据不会插入到我的表中。非常感谢!

<?php

include("class.php");

$dbhost     = "localhost";
$dbname     = "db";
$dbuser     = "user";
$dbpass     = "pass";

$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);

$username = $_POST['username'];
$password = $_POST['password'];
$email = strtolower($_POST['email']);
$firstName = ucwords(strtolower($_POST['firstName']));
$lastName = ucwords(strtolower($_POST['lastName']));
$date = date("Y-m-d");
$hash = Secure::Encrypt($username, $password);

$sql = "INSERT INTO users (username,password,email,firstName,lastName,createDate) VALUES (:username,:password,:email,:firstName,:lastName,:date)";
$q = $conn->prepare($sql);
$q->execute(array(
':username'=>$username,
':password'=>$hash,
':email'=>$email,
':firstName'=>$firstName,
':lastName'=>$lastName,
':date'=>$date));

?>

2 个答案:

答案 0 :(得分:2)

如果你的所有表的字段名都匹配,我发现如果你的一个数组变量为空,PDO有时会失败。

要查明是否有任何错误被抛出,请在new PDO声明后添加以下内容(默认情况下PDO错误报告是静默的):

$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

然后在每个PDO活动周围添加try / catch,如下所示:

try {
    ..Code Here...
}catch (PDOException $err) {
    echo $err->getMessage();
}

最终代码:

<?php

include("class.php");

$dbhost     = "localhost";
$dbname     = "db";
$dbuser     = "user";
$dbpass     = "pass";

$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$username = $_POST['username'];
$password = $_POST['password'];
$email = strtolower($_POST['email']);
$firstName = ucwords(strtolower($_POST['firstName']));
$lastName = ucwords(strtolower($_POST['lastName']));
$date = date("Y-m-d");
$hash = Secure::Encrypt($username, $password);

$sql = "INSERT INTO users (username,password,email,firstName,lastName,createDate) VALUES (:username,:password,:email,:firstName,:lastName,:date)";
try {    
    $q = $conn->prepare($sql);
 }catch (PDOException $err) {
    echo 'Prepare Failed: '.$err->getMessage();
 }
try {    
    $q->execute(array(
    ':username'=>$username,
    ':password'=>$hash,
    ':email'=>$email,
    ':firstName'=>$firstName,
    ':lastName'=>$lastName,
    ':date'=>$date));
 }catch (PDOException $err) {
    echo 'Execute Failed: '.$err->getMessage();
 }

?>

然后请更新您收到的任何错误:)

答案 1 :(得分:-3)

首先,将所有可能出错的内容插入try-catch块(例如数据库连接)中是一个好习惯

try {
    $conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDO Exception $e) {
    return $e;
}

您的sql语句有错误。您需要将表和列的名称放在反引号(`)

INSERT INTO `users` (`username`,`password`,`email`,`firstName`,`lastName`,`createDate`) VALUES (:username,:password,:email,:firstName,:lastName,:date)

最终代码:

<?php

include("class.php");

$dbhost     = "localhost";
$dbname     = "db";
$dbuser     = "user";
$dbpass     = "pass";

try {
    $conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDO Exception $e) {
    return $e;
}

$username = $_POST['username'];
$password = $_POST['password'];
$email = strtolower($_POST['email']);
$firstName = ucwords(strtolower($_POST['firstName']));
$lastName = ucwords(strtolower($_POST['lastName']));
$date = date("Y-m-d");
$hash = Secure::Encrypt($username, $password);

$sql = "INSERT INTO `users` (`username`,`password`,`email`,`firstName`,`lastName`,`createDate`) VALUES (:username,:password,:email,:firstName,:lastName,:date)";
$q = $conn->prepare($sql);
$q->execute(array(
':username'=>$username,
':password'=>$hash,
':email'=>$email,
':firstName'=>$firstName,
':lastName'=>$lastName,
':date'=>$date));

?>