PHP PDO 连接失败

发布于 2024-10-26 14:21:20 字数 902 浏览 2 评论 0原文

嘿,我是 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));

?>

Hey, so I am new with PDO, and I cannot figure out why my data will not insert into my tables. Much appreciated!

<?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));

?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷月断魂刀 2024-11-02 14:21:20

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

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

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

然后添加 try/catch 围绕每个 PDO 活动如下:

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();
 }

?>

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

If all of your table's field names match, I have found that PDO will sometimes fail if one of your array variables are empty.

to find out if any errors are being thrown, add the following after your new PDO declaration (PDO error reporting is silent by default):

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

Then add try/catch around each PDO activity as follows:

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

Final Code:

<?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();
 }

?>

Then please update with any errors you receive :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文