你好,我写了下面的代码
的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] => [email protected] [password] => test[confirm_password] => test[service] => pizza [phone_number] => 01234 )
当我执行我的代码时。但是我的数据库中什么也没有。我的错误在哪里?
您仅在连接失败时捕获异常,而不是在实际使用execute插入时捕获异常。要么使用两个@ try / catch,要么扩大try的范围并移动您的catch。
<?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());
}
?>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句