Php - 如何检查行是否存在并根据结果显示不同的内容?

时间:2014-04-30 20:33:26

标签: php

我有一个吸引页面,显示数据库中的一些信息,我在每个显示屏上都放了一个喜欢的按钮。这是我的代码:

$villageId = $_GET['village']; 
$sql    = "SELECT `AttractionID`, `Name`, `Location`, `Description`, `Airport`, `imglink`,`imglink2`,`imglink3`,`imglink4`,`imglink5`  FROM `attractions` WHERE `AttractionID`='$villageId'";

$result = mysql_query($sql, $link);

if (!$result) {
    echo "DB Error, could not query the database\n";
    echo 'MySQL Error: ' . mysql_error();
    exit;
}



while ($row = mysql_fetch_assoc($result)) {
    echo "<div class='attractions6'>";
    echo "<div class='attractions5'>";
    echo $row['Name']; 

    echo "<span title='Add to Favorites'>" . "<a href='favourite.php?village=" . $row['AttractionID'] . "'>" . 

 "<img src='img/fav.png'></a>";

    echo  "</span>" ;
    echo "<br />" ;
    echo "<br />";
    echo "</div>";

    echo $row['Description'];
    echo "<br />" ;

    echo "<br />";
    echo " Location: ";

    echo $row['Location'] ." ";
    echo "<br />" ;
    echo "<br />";

    echo " Nearest Airport: ".$row['Airport'] ." ";
    echo "<br />" ;

    echo "<br />";
    echo "</div>";
    echo '<a href="'.$row['imglink'].'" target="_blank"><img src="'.$row['imglink'].' " height="200" width="550"></a>';
    echo "<br />";
    echo '<a href="'.$row['imglink2'].'" target="_blank"><img src="'.$row['imglink2'].' " height="180" width="180"></a>';
    echo "&nbsp";
    echo '<a href="'.$row['imglink3'].'" target="_blank"><img src="'.$row['imglink3'].' " height="180" width="180"></a>';
    echo "&nbsp";
    echo '<a href="'.$row['imglink4'].'" target="_blank"><img src="'.$row['imglink4'].' " height="180" width="180"></a>';
    echo "&nbsp";
    echo '<a href="'.$row['imglink5'].'" target="_blank"><img src="'.$row['imglink5'].' " height="180" width="180"></a>';



    echo "&nbsp;";
    //This is the favorite button. If the user clicks on it the script 
    //will redirect to the favourite.php page where the database will be 
    //checked for duplicates in the attractionID field and will save the favorite. 

    echo "<br />";
    echo "<br />";

}

mysql_free_result($result);
?>

我在网站的其他地方使用以下代码,可以检查输入是否已存在:

$sql="SELECT `ID`,`AttractionID`  FROM `favourites` 
      WHERE `ID`='$ID' AND `AttractionID`='$villageId'  ";
$result = mysqli_query($con,$sql);

if ($result->num_rows > 0) {
    header('location:attraction.php?village='.$villageId);
}

在景点页面上我该如何制作,以便如果用户已将此条目保存到收藏夹 - 我将显示picture1,如果他们没有 - 我将显示图片2?

感谢。

0 个答案:

没有答案