您好我写了以下代码
PHP
<?php
try{$bdd = new PDO('mysql:host=localhost;dbname=test', 'root', '');}
catch (Exception $e){die('Error: ' . $e->getMessage());}
$req = $bdd->prepare('insert into signup
(email, password, service, phone)
values(:email, :password, :service, :phone_number) ');
$req->execute(array(
'email' => htmlspecialchars($_POST['email']),
'password' => htmlspecialchars($_POST['password']),
'service' => htmlspecialchars($_POST['service']),
'phone_number' => htmlspecialchars($_POST['phone_number'])));
echo 'Well done';
print_r($_POST);
?>
我有一个“做得好”的消息和
Array ( [email] => test@test.test [password] => test[confirm_password] => test[service] => pizza [phone_number] => 01234 )
当我执行我的代码时。但我的数据库中没有任何东 我的错误在哪里?
答案 0 :(得分:0)
如果连接失败,则只捕获异常,而不是在使用execute实际插入时捕获异常。要么使用两个@ try / catch,要么扩大try&amp;的范围。移动你的渔获。
<?php
try
{
$bdd = new PDO('mysql:host=localhost;dbname=test', 'root', '');
$req = $bdd->prepare('insert into signup
(email, password, service, phone)
values(:email, :password, :service, :phone_number) ');
$req->execute(array(
'email' => htmlspecialchars($_POST['email']),
'password' => htmlspecialchars($_POST['password']),
'service' => htmlspecialchars($_POST['service']),
'phone_number' => htmlspecialchars($_POST['phone_number'])));
echo 'Well done';
print_r($_POST);
}
catch (Exception $e)
{
die('Error: ' . $e->getMessage());
}
?>