我一直致力于建立类似于facebook的社交网络,以便在我的家庭服务器上使用。但是,我在用户配置文件的通知选项卡中显示好友请求时遇到了问题。
<?php
//Check for notifications script
$Request = "Requests";
$Pending = "Pending";
$nCheck = mysqli_query($Connect,"SELECT * FROM friends WHERE rTo = '$Uname' AND Status = '$Pending'");
$nNum = mysqli_num_rows($nCheck);
if($nNum != 0){
if($nNum === 1){
$Request = "Request";
}
else
{
$Request = "Requests";
}
echo"<p style=\"font-family: Tahoma; margin-left: 20px;\">You Have <b>$nNum</b> Friend $Request</p><hr />";
while($row = mysqli_fetch_assoc($nCheck)){
$dbFrom = $row['rFrom'];
$dbId = $row['id'];
$dbStatus = $row['Status'];
for($i = 0; $i < 1; $i++){
echo"<p style=\"font-family: Tahoma; margin-left: 20px;\">$dbFrom wants to be your friend <form action=\"\" method=\"POST\"><input type=\"submit\" name=\"A\" value=\"Accept\"></form> <form action=\"\" method=\"POST\"><input type=\"submit\" name=\"D\" value=\"Decline\"></form></p>";
}
}
}
else
{
echo "<p style=\"font-family: Tahoma; margin-left: 20px;\">You Have <b>0</b> Friend $Request</p>";
}
?>
然后这是接受/拒绝的表单:
<?php
//Accept/Decline Friend Request Script
$Accepted = @$_POST['A'];
$Declined = @$_POST['D'];
$A = "Accepted";
$D = "Declined";
if($Accepted){
$addFriend = mysqli_query($Connect,"UPDATE friends SET Status = '$A' WHERE rTo = '$Uname' AND rFrom = '$dbFrom'");
header("Location: Notification.php");
}
else if($Declined){
$addFriend = mysqli_query($Connect,"UPDATE friends SET Status = '$D' WHERE rTo = '$Uname' AND rFrom = '$dbFrom'");
header("Location: Notification.php");
}
?>
问题是,如果存在多个通知,则最后一个通知将是接受或拒绝的通知。请帮助,非常感谢。
答案 0 :(得分:3)
'
以避免必须在源代码中转义HTML的所有双引号"
。字符串连接的替代方法通常在编码指南中推荐。*
。例如。您不需要$row['Status']
和$row['id']
,但仍然无缘无故地将其保存在变量中。@
运算符。改为验证您的POST数据。答案 1 :(得分:1)
如果两个代码示例都存在于一个文件中,那么这可以解释您的问题。 您更新好友表:
$addFriend = mysqli_query($Connect,"UPDATE friends SET Status = '$A' WHERE rTo = '$Uname' AND rFrom = '$dbFrom'");
如果$dbFrom
值来自:
while($row = mysqli_fetch_assoc($nCheck)){
$dbFrom = $row['rFrom'];
这意味着您只需更新最后一行。
我建议你发布$dbForm
这两个表格。
PS:并开始使用PDO。
答案 2 :(得分:0)
也 尝试使用这个
$A = "Accepted";
$D = "Declined";
if($Accepted){
$addFriend = mysqli_query($Connect,"UPDATE friends SET Status = '$A' WHERE id='$dbId' AND rFrom = '$dbFrom'");
header("Location: Notification.php");
}
else if($Declined){
$addFriend = mysqli_query($Connect,"UPDATE friends SET Status = '$D' WHERE id='$dbId' AND rFrom = '$dbFrom'");
header("Location: Notification.php");
}