有谁知道为什么这段代码无效,它会显示取消关注位,但不显示以下部分。所有它应该做的就是如果他们不跟随那个人就说跟随,如果他们是,则取消关注。我尝试过使用Just if(row['following'])
但它有相同的输出。
谢谢
<?php
$con=mysqli_connect("localhost","Username","Password","DB");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
session_start();
$follower=$_SESSION['user']['id'];
$following=$_GET['id'];
$result = mysqli_query($con,"SELECT * FROM following WHERE follower='$follower' AND following='$following'");
while($row = mysqli_fetch_array($result)) {
if(isset($row['following'])){
?>
<a href="/profile/unfollow.php/?id=<?php echo $dnn['id']; ?>">
<?php
echo'<div id="button">';
echo'Unfollow <?php echo $dnn["username"]; ?>';
echo'</div>';
}
else{
?>
<a href="/profile/follow.php/?id=<?php echo $dnn['id']; ?>">
<?php
echo'<div id="button">';
echo'Follow <?php echo $dnn["username"]; ?>';
echo'</div>';
}
mysqli_close($con);
}
?>
答案 0 :(得分:3)
$row['following']
。你想要的是检查它是否有值:
if(!empty($row['following'])){
仅供参考,您对SQL injections
持开放态度答案 1 :(得分:2)
<?php
/* ESTABLISH CONNECTION */
$con=mysqli_connect("localhost","Username","Password","DB");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
session_start();
$follower=mysqli_real_escape_string($con,$_SESSION['user']['id']); /* ESCAPE STRING */
$following=mysqli_real_escape_string($con,$_GET['id']); /* ESCAPE STRING */
$result = mysqli_query($con,"SELECT * FROM following WHERE follower='$follower' AND following='$following'"); /* EXECUTE QUERY */
while($row = mysqli_fetch_array($result)){
if(!empty($row['following'])){ /* IF FOLLOWING IS NOT EMPTY */
$id=mysqli_real_escape_string($con,$row['id']);
$username=mysqli_real_escape_string($con,$row['username']);
echo "<a href='profile/unfollow.php?id=$id'>";
echo '<div id="button">';
echo 'Unfollow '.$username;
echo '</div>';
} /* END OF IF NOT EMPTY FOLLOWING */
else { /* ELSE IF EMPTY */
$id=mysqli_real_escape_string($con,$row['id']);
$username=mysqli_real_escape_string($con,$row['username']);
echo "<a href='profile/follow.php?id=$id'>";
echo '<div id="button">';
echo 'Follow '.$username;
echo '</div>';
} /* END OF ELSE */
} /* END OF WHILE LOOP */
mysqli_close($con);
?>
mysqli_close($con)
移到while循环之外。我很确定你的循环会在第一个循环停止。