为什么我的PHP别人没有显示?

时间:2014-06-05 22:04:59

标签: php mysql

有谁知道为什么这段代码无效,它会显示取消关注位,但不显示以下部分。所有它应该做的就是如果他们不跟随那个人就说跟随,如果他们是,则取消关注。我尝试过使用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);
}
?>

2 个答案:

答案 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循环之外。我很确定你的循环会在第一个循环停止。
  • 你的while循环中的 dnn [] 是什么?用 row [] 替换它。
  • 使用 mysqli_real_escape_string 函数来阻止某些 SQL注入
  • isset()函数替换为 empty()函数
  • 上述代码中的/ * * /中引用了更多解释