php mysql +创建一个好友请求系统

j5fpnvbx  于 2022-12-21  发布在  PHP
关注(0)|答案(2)|浏览(117)

我正在创建一个朋友请求系统,允许用户发送请求给对方添加为朋友,如Facebook,我有2个条件,如果:

  • 用户是配置文件所有者,他不能添加他自己,我将回显一个错误消息
  • 用户不是配置文件的所有者,它将回显一条消息,表示等待,然后发送请求

如果用户已经发送了请求,则我将回显消息以通知用户已经发送了请求。
但错误是,我在第一个条件,因为系统显示用户是所有者配置文件,但这是错误的
有谁能帮帮我吗?

这是一段代码

function addAsFriend(a,b){
   //alert("Member with id:" + a + "request friendship with the memeber with id:" + b);
  var url = "script_for_profile/request_as_friend.php";
  $("#add_friend").text("please wait...").show();
  $.post(url,{request:"requestFreindship",mem1:a,mem2:b},function(data){
    $("#add_friend").html(data).show().fadeOut(12000);
  });
}

<div class="interactContainers" id="add_friend">
               <div align="right"><a href="#" onclick="return false" onmousedown="javascript:toggleInteractContainers('add_friend');">Cancel</a></div>
               Add <?php echo $username ?> as Friend?&nbsp;
               <a href ="#" onclick="return false" onmousedown="javascript:addAsFriend(<?php echo $_SESSION['user_id']; ?>,<?php echo $userid; ?>);">Yes</a>

请求作为朋友. php

<?php
//var we need for  both  members
$mem1=preg_replace('#[^0-9]#i','',$_POST['mem1']);
$mem2=preg_replace('#[^0-9]#i','',$_POST['mem2']);

if(!$mem1||!$mem2)
{
    echo "Error .missing data";
    exit();
}
if($mem1==$mem2)
{
    echo "Error you can not add yourself as friend";
    exit();
}
 require_once('../include/connect.php'); 

 if($_POST['request']=="requestFriendship")
 {
     //check that  there  is not  a request  pending  where this viewer  is requesting  this profile  owner
     $sql = mysql_query("SELECT id  FROM friend_requests WHERE mem1='$mem1' AND mem2='$mem2'limit1")or die(mysql_error());
     $numRows = mysql_num_rows($sql);
     if($numRows > 0)
     {
         echo "You have a friend request pending already  for this member. they must approve  it when they view their request list";
         exit();
     }
      //check that  there  is not  a request  pending  where this profile owner  is not already requesting  this viewer
     $sql = mysql_query("SELECT id  FROM friend_requests WHERE mem1='$mem2' AND mem2='$mem1'limit1")or die(mysql_error());
     $numRows = mysql_num_rows($sql);
     if($numRows > 0)
     {
         echo "This user has requested you as friend already! Check  your friend  Request on your profile";
         exit();
     }
     $sql = mysql_query("INSERT INTO friend_requests(mem1,mem2,timedate) VALUES('$mem1','$mem2',now())") or die (mysql_error("friend request  insertionn error"));
     //$sql = mysql_query("INSERT INTO pms(to,from,time,sub,msg) VALUES('$mem2','XXXXX',now(),'New Friend Request','YOU Have a New  friend request waiting for approval.<br /><br />Navigate to your profile and check  your friend request.<br /><br />Thank you.')") or die (mysql_error("friend request PM insertionn error"));

  echo "Friend Request  sent  succesfully. this member must approve the request";
  exit();    
 }
?>
kfgdxczn

kfgdxczn1#

我完全错了,你不需要创建任何额外的标志,只需要存储user_id-* 用户登录时从数据库中检索到的 * -存储在会话中,然后当他/她点击添加好友按钮时,在完成友谊功能之前用其他用户的ID检查$_SESSION['user_id'],如果两者相同,则意味着是同一个人,否则添加好友。

wvt8vs2t

wvt8vs2t2#

post数组为mem1和mem2返回什么?
然而,你不应该比较帖子数据,或者两者都不应该。
例如,您已登录,您的ID存储在会话中。您正在打开用户配置文件,即:http://yoursite.com/viewprofile.php?id=1001。然后在从jquery传递后,您应该检查PHP smth,如下所示:

if ($_GET['id'] = $_SESSIOM['id']) {
   //you cannot add yourself
}

相关问题