i am creating a social site using PHP and HTML. i want to add "ADD FRIEND" button or link on my site. I wonder how can i do that. means whenever user searches for some name, then related search get displayed sequentially.
What i want is add friend button in front of every user that listed by search.. is there any way i can do that?
so if user click on that button then user id and friend id get inserted into friend table?
Thanks in Advance.
Ok, I've done this, first of all set a session variable and store current logged in user id in that like this
$_SESSION['user_id'] = //loggedin user id
now I am not aware what kind of database design are you using, or what because you've not specified or provided any code from your side but simple way you can do this is make a simple submit button encapsulate it within a form tags, and use post method
<?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
}
?>