【发布时间】:2020-07-22 16:04:09
【问题描述】:
我希望如果数据库中已经存在“tags_name”,它将在 php 中显示错误而不是fatel错误消息。
if(isset($_POST['NewTag']))
{
$tags_name= $_POST['tags_name'];
$stmt = $DBcon->prepare("INSERT INTO category (tags_name) VALUES(:tags_name)");
$stmt->bindparam(':tags_name', $tags_name);
if ($stmt->execute()) {
$successMSG = "Succesfull";
} else {
$errMSG = "Entry already exists";
}
}
我目前收到此错误“致命错误:未捕获的 PDOException:SQLSTATE[23000]:完整性约束违规:1062 键 'tags_name' 的重复条目 'testing' ....”而不是我的 errMSG。
【问题讨论】:
-
确保您转义来自用户的输入,这可能对您有用stackoverflow.com/a/3146986/9201277
标签: sql if-statement