please correct this problem

id -- int
user_id -- int 
friend_id -- int
<?
session_start(); //starts session
mysql_connect("localhost", "root", "");
mysql_select_db("admin");



if ($user_id= $_SESSION['logged']){ //check if user is logged in

if($_GET['user_id']){ //gets userid 
$friend_id=$_GET['user_id']; //friend
$by = $user_id; //me
$query = "INSERT INTO friend_req ( 'friend_id , 'user_id' ) VALUES ( '$friend_id' , '$user_id' )"; //inserts the request
$result = mysql_query($query) or die(mysql_error());

echo ( "Friend request sent" ); //echo completion
} 
else {
echo ( "No request was made" ); // or no request sent
}
}
else {
echo ( "You need to be logged in" ); //not logged in
}
?>

<form action = 'friend_request.php?user_id=<?php echo $user_id= $_SESSION['logged']?>' method='post'>
    <input type = 'submit' value = 'Add as Friend'>

Recommended Answers

All 5 Replies

What is the problem with your code?Can you explain it?

code not display any option like that freind request send. and nothing happens when i press add freind button

please correct this problem

id -- int
user_id -- int 
friend_id -- int
<?
session_start(); //starts session
mysql_connect("localhost", "root", "");
mysql_select_db("admin");



if ($user_id= $_SESSION['logged']){ //check if user is logged in

if($_GET['user_id']){ //gets userid 
$friend_id=$_GET['user_id']; //friend
$by = $user_id; //me
$query = "INSERT INTO friend_req ( 'friend_id , 'user_id' ) VALUES ( '$friend_id' , '$user_id' )"; //inserts the request
$result = mysql_query($query) or die(mysql_error());

echo ( "Friend request sent" ); //echo completion
} 
else {
echo ( "No request was made" ); // or no request sent
}
}
else {
echo ( "You need to be logged in" ); //not logged in
}
?>

<form action = 'friend_request.php?user_id=<?php echo $user_id= $_SESSION['logged']?>' method='post'>
    <input type = 'submit' value = 'Add as Friend'>

Whether it showing "No request was made" option?
Check out the database connection using following code

<?php
$con = mysql_connect('localhost','root','');
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
else
{
 echo "Connected";
}
?>

Print the value of the variables like user_id,$friend_id.

Check line 11 and 12. You use post method in form. But, you get with GET method.

if($_GET){ //gets userid
$friend_id=$_GET;

Member Avatar for diafol

Maybe your table structure's not right. How about:

requester_id | acceptor_id | daterequest | status

Obviously, the first two fields are foreign fields of your user_id in your user table. The third is the date when request made (so you can resend the request at certain intervals, e.g. every three days if not accepted (1).

status, e.g. 0 = awaiting reply, 1 = accepted, 2 = rejected

BTW first two fields together are PK, so no duplicates.

My 2p.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.