user457666
user457666

Reputation: 73

retrieving information from mysql return values using $_SESSION

I have to create a database for a class and I am having trouble with one thing. In the database, a user can search for other users and choose to friend them (just like facebook). In my php scripts, I am searching my 'user' table (in MySQL) for all users that match a string entered in by the logged-in user, and providing a list of names that match the string. The logged in user can then click on the name they would like to friend, and a request is sent to that person. I am not sure how to retrieve the username and id of the name that is clicked on. For example, if the list of names that are displayed is:

Sally
Kevin
Mike

and the user clicks on Sally, how can I retrieve her username and id? In the sql query I am using, the last person is at the top of the array, so they logged in user always tries to friend Mike. Any suggestions would be great!!

The $_SESSION values are taking the last name in the array. How can I get it to take the name the logged-in user clicks on?

Or is there a better way to do this?

Here is my code so far:

$sql="SELECT uid, name FROM user WHERE name LIKE '%" . $name . "%'";

//run  the query against the mysql query function
$result=mysql_query($sql);

//create  while loop and loop through result set
while($row=mysql_fetch_array($result)){
        $name=$row[name];
        $uid=$row[uid];

        //display the result of the array
        echo "<ul>\n";
        echo "<a href=\"friend_request.php\">" .$name. "</a>";
        echo "</ul>";
        $_SESSION['friend_name'] = $name;
        $_SESSION['friend_id'] = $uid;   
}

Upvotes: 0

Views: 331

Answers (3)

tHeSiD
tHeSiD

Reputation: 5333

I see you are generating the url with a name which in your case is not a key in the database. Why not send the UID itself to the friend_request.php and then use that UID to send the request to that person.

 echo "<a href=\"friend_request.php?uid=".$uid."&name=".$name.">" .$name. "</a>";

Remove the $_SESSION variable, you dont need it.

In the friend_request.php, change $_SESSION['uid'] and $_SESSION['name'] to $_GET['uid'] and $_GET['name']

Upvotes: 3

Radi
Radi

Reputation: 6584

it depends on how you generate the persons list that user want to chose from so the list may be like :

<a href="http://..../makefriend.php?name=sally">sally</a>

and in your php code aka makefriend.php you can get the user id and name from the db like :

$result = mysql_query("SELECT uid, name FROM user WHERE name LIKE '%" . $_get['name'] . "%' 

i hope this can help .

Upvotes: 0

David
David

Reputation: 218808

You should pass the value of the friend being selected to the request that's selecting it, rather than trying to store it in session. Something like this:

echo "<a href=\"friend_request.php?name=" .$name. "\">" .$name. "</a>";

That way the value selected will be available to the friend_request.php script as $_GET['name'].

Upvotes: 0

Related Questions