Friend Request Script

I’m putting together a small network script to learn how it’s done. I am running into issues with the “add” script, when a user chooses a friend to add. Can anyone tell me what I might be doing wrong here, or point me in the right direction? All I’m trying to do is run a database query that inserts the users as friends when they press a button. Can you do it in this way! Many thanks for any help =)

[php]<?php // This is how a user can add a friend to view their profile

error_reporting (‘E_ALL’);

// Start the session…
session_start();

// Require a connection to the database…
require_once(‘mysqli_connect.php’);

// Store the user id from sessions as a variable…
$id = $_SESSION[‘user_id’];

if (isset($_POST[‘submitted’])) {

// What is the id of the person this user wants to add?
$mem_id = '$_GET['user_id']';


// Insert this friendship into the database until the other user accepts it...
$q = "INSERT INTO friends (friend_id, user_id, status) VALUES ('$mem_id', '$id', '0') LIMIT 1";
$r = @mysqli_query ($dbc, $q);


// Some action needs to take place here where it shows up on the other user's profile...
$q = "SELECT email FROM users WHERE user_id = '$mem_id'";
$r = @mysqli_query ($dbc, $q);

// Here is the member's email to send the confirmation email to the other user...
$email = mysqli_fetch_row ($r);

// Now send the email...
$body = "You have a friend request. To respond to this request, login and check your requests.";
mail ($trimmed['email'], 'Friend Request', $body, 'From: [email protected]');

}

?>

    <form enctype="text/plain" action="add.php" method="post">
    <input type="submit" name="submit" value="Add a Friend"/>
    <input type="hidden" name="submitted" value="TRUE"/>
	</form>[/php]

Your form method is POST, and your action url is add.php… Where is this parameter set:
[php]$_GET[‘user_id’][/php]
?

You can add a hidden field to your form to store user_id, but in this case, you will find it in your script in the $_POST array:
[php]$mem_id = $_POST[‘user_id’];[/php]

Hi, thanks for your reply. I would appreciate a bit of clarification. The $_GET[‘user_id’] parameter is set in the URL of the page. This script is sort of embedded into the user profile page, and the user ID of the person who’s profile you are viewing is in the URL, therefore the $_GET parameter is retrieved from the URL.

I am not sure if I can use an HTML form to add a friend? Could you point me in the right direction as to how this can be accomplished?

Kind regards…

Sponsor our Newsletter | Privacy Policy | Terms of Service