1

我正在使用 PHP 和 HTML 创建一个社交网站。我想在我的网站上添加“添加朋友”按钮或链接。我想知道我该怎么做。意味着每当用户搜索某个名称时,相关搜索就会按顺序显示。

我想要的是在搜索列出的每个用户前面添加朋友按钮..有什么办法可以做到吗?

因此,如果用户单击该按钮,则用户 ID 和朋友 ID 会被插入到朋友表中?

提前致谢。

4

1 回答 1

2

好的,我已经这样做了,首先设置一个会话变量并将当前登录的用户 ID 存储在其中

$_SESSION['user_id'] = //loggedin user id

现在我不知道您使用的是哪种数据库设计,或者因为您没有指定或提供任何代码,但您可以简单的方法是制作一个简单的提交按钮将其封装在表单标签中,并且使用 post 方法

<?php
if(isset($_POST['add_friend'])) {
$current_user = $_SESSION['user_id']; //This will fetch user id of the person who is logged in and for this you need to have a user id in your session for the user who logs in

$friendid = $_POST['friend_id']; //This will assign friend id to variable $friendid

//Now execute the insert statement
if(mysqli_query($connect, "INSERT INTO addfriendtable(user_id, friend_id) VALUES('$current_user','$friendid')")) {
echo 'success';
} else {
echo 'Error Occured';
}
}

$fetch_records = mysqli_fetch_array($connect, "SELECT name, userid FROM table");
while ($show_users = mysqli_fetch_array($fetch_records)) {
//loop all search results
//place this code in front of all results
?>
<form method="post">
<input type="hidden" value="<?php echo $show_users['user_id']; ?>" name="friend_id">
<input type="submit" name="add_friend" value="Add Friend" />
</form>
<?php
}
?>
于 2012-10-01T06:01:25.873 回答