我正在尝试检查并确定数据库中是否存在该值,以便我可以执行此行代码。
数据库内容
我的依据是moviesId
表字段。因此,当我单击模式11时,如果它确实存在于数据库中,则应该显示链接不喜欢的,反之亦然
$data = mysqli_query($con,"SELECT * FROM movies_favorites where moviesId= '$moviesid'");
$count = mysqli_num_rows($data);
if ($count > 0) {
echo '<a class="btn btn-sm btn-dark" data-toggle="tooltip" data-placement="right" title="Favorites" href="movies-modal.php?moviesid='.$row2['id'].'&&action=moviesfavorites">
<i class="fa fa-heart"></i> Favorite
</a>';
} else {
echo '<a class="btn btn-sm btn-dark" data-toggle="tooltip" data-placement="right" title="DeleteFavorites" href="movies-modal.php?moviesid='.$row2['id'].'&&action=deletefavorites">
<i class="fa fa-heart-o"></i> Unfavorite
</a>';
}
?>
如果该值存在,则将显示Favorite
链接,如果不存在,则将显示Unfavorite
链接。但是现在我什么也没得到。而“不喜欢”链接是即使没有数据也显示的链接。
预期产量
当数据存在或不存在时显示不喜欢链接,显示收藏链接。
答案 0 :(得分:0)
Expected Output - display Unfavorite link when data exists and
when not Favorite link will display.
那是你的问题。但是您的代码逻辑相反。用以下代码替换整个代码段:
$data = mysqli_query($con,"SELECT * FROM movies_favorites where moviesId= '$moviesid'");
/* If $data exists */
if ($data) {
if (mysqli_num_rows($data) > 0) {
/* Displays Unfavorite if $data is not empty */
echo '<a class="btn btn-sm btn-dark" data-toggle="tooltip" data-placement="right" title="DeleteFavorites" href="movies-modal.php?moviesid='.$row2['id'].'&&action=deletefavorites">
<i class="fa fa-heart-o"></i> Unfavorite
</a>';
}
else {
/* Displays Favorite if $data is empty */
echo '<a class="btn btn-sm btn-dark" data-toggle="tooltip" data-placement="right" title="Favorites" href="movies-modal.php?moviesid='.$row2['id'].'&&action=moviesfavorites">
<i class="fa fa-heart"></i> Favorite
</a>';
}
}
?>
在执行if-else代码之前,请先检查由$data
返回的mysqli_query
是否存在。我对您的代码做了一些修改,并交换了您的条件。
答案 1 :(得分:0)
您应该先查找查询返回的数据。
仅当查询返回的数据不是null
时才运行代码。
因此,首先检查结果是否不为null,然后再继续。
您可以参考以下代码:
$data = mysqli_query($con,"SELECT * FROM movies_favorites where moviesId= '$moviesid'");
//check if $data is not null
if (true == $data){
//further processing continue
if(mysqli_num_rows($data) <= 0) {
echo '<a class="btn btn-sm btn-dark" data-toggle="tooltip" data-placement="right" title="Favorites" href="movies-modal.php?moviesid='.$row2['id'].'&&action=moviesfavorites">
<i class="fa fa-heart"></i> Favorite
</a>';
} else {
echo '<a class="btn btn-sm btn-dark" data-toggle="tooltip" data-placement="right" title="DeleteFavorites" href="movies-modal.php?moviesid='.$row2['id'].'&&action=deletefavorites">
<i class="fa fa-heart-o"></i> Unfavorite
</a>';
}
}
?>
答案 2 :(得分:0)
为了安全性/稳定性,您确实应该使用准备好的语句。
我假设您需要WHERE逻辑中的另一个条件才能将movieId与userId关联起来。
这是未经测试的代码段:
if (!$stmt = $conn->prepare("SELECT COUNT(*) FROM movies_favorites WHERE userId=? AND moviesId=?")) {
echo "Prepare Syntax Error: " , $conn->error; // don't show this to the public
} else {
if (!$stmt->bind_param("ii", $userid, $movieid) // if trouble while binding to ? placeholder
|| !$stmt->execute() // or if trouble while executing
|| !$stmt->bind_result($num_rows) // or if trouble while binding to $num_rows
|| !$stmt->fetch()) { // or if trouble while fetching the one row.
echo "Statement Error: " , $stmt->error; // don't show this to the public
} else {
if ($num_rows) { // if not zero, enable deletion
$title = "DeleteFavorites";
$action = "deletefavorites";
$icon = "fa-heart-o";
$text = "Unfavorite";
} else { // if zero, allow to add
$title = "Favorites";
$action = "moviesfavorites";
$icon = "fa-heart";
$text = "Favorite";
}
$stmt->close(); // no longer need statement
echo "<a class=\"btn btn-sm btn-dark\" data-toggle=\"tooltip\" data-placement=\"right\" title=\"{$title}\" href=\"movies-modal.php?moviesid={$row2['id']}&action={$action}\">";
echo "<i class=\"fa {$icon}\"></i> $text";
echo "</a>";
}
}
如果我的逻辑相反,那么您的问题要求只会使我感到困惑...如果您发现有什么问题,我可以很高兴地编辑我的答案以进行修正。
答案 3 :(得分:-1)
$data = mysqli_query($con,"SELECT * FROM movies_favorites where moviesId= '$moviesid'");
您可以那样更改代码,然后重试吗?
$data = mysqli_query($con,"SELECT * FROM movies_favorites WHERE moviesId= '".$moviesid."'");
或
$data = mysqli_query($con,"SELECT * FROM movies_favorites WHERE moviesId= '{$moviesid}'");