0
我真的很困擾這一段時間,似乎無法讓它工作。在members.php中(我顯示所有註冊用戶),我有一個列表打印出一個鏈接「添加到朋友」旁邊的每個用戶。找不到朋友的ID將它存儲到數據庫中
我管理,出於測試的目的,以顯示每個成員身份證好(所以它得到的ID),但當我點擊鏈接,它指向的好友似乎是錯誤的地方。我不知道如何獲得朋友的ID我點擊了在friends.php文件中。請看看!
members.php
<?php
include 'connect.php';
include 'header.php';
if(isset($_SESSION['signed_in']) == false || isset($_SESSION['user_level']) != 1)
{
//the user is not an admin
echo '<br/>';
echo 'Sorry! You have to be <a href="/signin.php"><b>logged in</b></a> to view all the <a href="signup_2.php" title="Become a registered user!"><b>registered</b></a> members.';
echo '<br/><br/>';
}
else
{
echo '<h2>Registered users:</h2>';
$sql = "SELECT * FROM users ORDER BY user_name ASC";
$result = mysql_query($sql);
$num=mysql_numrows($result);
$i=0;
while ($i < $num)
{
//$name = mysql_result($result,$i,"user_name");
//$id = mysql_result($result,$i,"user_id");
//$picture = mysql_result($result,$i,"pic_location");
//?friend_id="'. $id .'
while($user = mysql_fetch_array($result)){
echo $user['user_name'].'<br/><br/><a href="friends.php?user='.$user['user_id'].'">ADD TO FRIENDS</a><br/>';
echo $user['user_id'];
echo '<br/><br/>';
}
$i++;
}
///////////////////////////////
/// adding users as friends ///
///////////////////////////////
//while($user = mysql_fetch_array($result))
//echo $user['user_name'].'
//<a href="addfriend.php?user='.$user['id'].'">ADD TO FRIENDS</a><br/>';
//NOW I WANT TO MAKE A SPECIFIC "ADD AS FRIEND" LINK NEXT TO EACH USER
}
include 'footer.php';
?>
正如我說我不知道如何得到這個,所以請看看!謝謝!
Ĵ
friends.php
<?php
include "connect.php";
include "header.php";
if(isset($_SESSION['signed_in']) == false || isset($_SESSION['user_level']) != 1)
{
//the user is not an admin
echo '<br/>';
echo 'Sorry! You have to be <a href="/signin.php"><b>logged in</b></a> if you want to add the person as a friend!';
echo '<br/><br/>';
}
else
{
$sql = "SELECT * FROM users";
$result = mysql_query($sql);
//friend_id is the ID of the friend that is clicked on...
//HOW DO I GET THAT ID THAT IS CLICKED ON IN THE WHILE "loop" in members.php?
$friends = ("INSERT INTO friends SET user_id='" . $_SESSION['user_id'] . "', friend_id='".$id."', status='0'");
$result_friends = mysql_query($friends);
if(!$friends)
{
//When you can't add this person as a friend this error will show!
echo 'You cannot add this user at this time. Please try again later!';
}
else
{
//When the friend is now added to the system!
echo 'Great! Now the user needs to approve before you can be friends!';
}
}
?>
我猜'$ _SESSION ['user_id']'保存訪問該頁面的用戶的ID,並且它被設置在別的地方。 '$ _GET ['user']'持有這個用戶想要添加的朋友的ID。 – AJJ
好酷!我理解得更好,但現在它說第20行中的C:\ xampp \ htdocs \ friends.php中的未定義索引:user_id。那麼,如何「聲明」該特定的user_id? –
@AJweb:你真棒!但我似乎無法得到它的工作! :( –