$db = new PDO('mysql:host=localhost;dbname=dbname;charset=utf8', 'username', 'password');
try
{
//Prepare and execute an insert into DB
$st = $db->prepare("INSERT INTO users(login,pass,email,county) VALUES(:username,:password,:email,:county)");
$st->execute(array(':username' => $_POST['username'], ':password' => $_POST['password1'], ':email' => $_POST['email'], ':county' => $_POST['county']));
echo 'Success';
}
catch (PDOException $e)
{
echo $e->getMessage();
}
嗨,我并不完全熟悉pdo,但我想我会添加一些错误异常,除非我不会实际显示错误,因为我不希望每个人都知道我的架构。在这种情况下,我通过将“...,county)”更改为“....,count)”将我的工作代码更改为非工作代码,这显然没有插入到数据库中,但仍然显示“成功”和没错。
请帮助:(
答案 0 :(得分:1)
您需要设置PDO错误模式以引发异常。
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);