php - PHP在数据库中验证是否存在重复
问题描述
我有这个功能:
public function addToFavoriteList($eventId, $userId)
{
$sql = "INSERT INTO favorites (eventi_id, user_id) VALUES ($eventId, $userId)";
$resultSet = $this->db->execute($sql);
if (!$resultSet) {
return array('error' => 'event exsist in favorites');
}
return array('error' => '');
}
我想检查事件是否已经存在于数据库中,以便我可以为用户显示错误消息。我写了类似的东西,但它不起作用。
if (isset($_POST['addToFavourite'])) {
$eventId = htmlspecialchars(trim($_POST['id']));
$addToFavoriteOutcome = $eventMgr->addToFavoriteList($eventId, $userId);
if (isset($addToFavoriteOutcome)) {
$errorMessage = $addToFavoriteOutcome['error'];
}
}
解决方案
尝试改变这个:
if (isset($addToFavoriteOutcome)) {
$errorMessage = $addToFavoriteOutcome['error'];
}
对此
if ($addToFavoriteOutcome) {
$errorMessage = $addToFavoriteOutcome['error'];
}
推荐阅读
- swift - 在 WKWebView evaluateJavaScript 方法上,无法监听 dom 事件
- c# - 使用 EWS 的自动发现无法正常工作,具体取决于正在运行的机器
- reactjs - ReactJs TypeError:映射时无法读取null的属性(读取'localeCompare')
- amazon-web-services - 如何配置 IAM 角色为新的 EC2 实例启用 SSM?
- react-native - 反应原生的滚动视图不滚动
- python - python中的JSON遍历和编辑
- c# - Rigidbody2D:如何防止 Y 轴被 0 覆盖?
- firebase - 正确使用异步/等待对 Firestore 数据库进行唯一用户名检查
- android - 当应用程序处于后台时,Firebase 通知不会出现(Android)
- python - 将 numpy 乘以数据帧,其中 numpy 中的索引号与数据帧值匹配