我是新手使用PDO,我在提交表单时收到http 500服务器错误.
带有处理代码的php页面在正确的文件夹中,所以我不知道它为什么会抛出500错误.
没有任何网址重写.
这是我的代码:
try {
$dbh = new PDO("mysql:host=$hostname;dbname=crm",$username,$password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line
$stmt = $db->prepare("INSERT INTO testdrives (forename, surname,phone,email,add1,add2,add3,city,county,postcode,car,date) VALUES (:forename, :surname,:phone,:email,:add1,:add2,:add3,:city,:county,:postcode,:car,:date)");
$stmt->bindParam(':forename', $_POST['forename']);
$stmt->bindParam(':surname', $_POST['surname']);
$stmt->bindParam(':phone', $_POST['phone']);
$stmt->bindParam(':email', $_POST['email']);
$stmt->bindParam(':add1', $_POST['add1']);
$stmt->bindParam(':add2', $_POST['add2']);
$stmt->bindParam(':add3', $_POST['add3']);
$stmt->bindParam(':city', $_POST['city']);
$stmt->bindParam(':county', $_POST['county']);
$stmt->bindParam(':postcode', $_POST['postcode']);
$stmt->bindParam(':car', $_POST['car']);
$stmt->bindParam(':date', $_POST['date']);
$stmt->execute();
if ($dbh->query($sql)) {
echo "<script type= 'text/javascript'>alert('New Record Inserted Successfully');</script>";
}
else{
echo "<script type= 'text/javascript'>alert('Data not successfully Inserted.');</script>";
}
$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
Run Code Online (Sandbox Code Playgroud)
编辑:
我错过了用于连接的错误变量$dbh和$db准备中; 我的错.
原始答案.
这一行:
if ($dbh->query($sql)) {...}
Run Code Online (Sandbox Code Playgroud)
失败有两个原因:
query()已经准备/执行的内容.$sql.用相关的括号去除该语句,并简单地替换它并替换为$stmt->execute();:
if($stmt->execute()){
// success
} else{
// error
}
Run Code Online (Sandbox Code Playgroud)
并使用PDO的错误处理(正如您现在所做)和PHP的错误报告:
还检查您的日志.
| 归档时间: |
|
| 查看次数: |
848 次 |
| 最近记录: |