2012-01-19 77 views
-2

編輯 - 我解決了我的「添加朋友」按鈕問題,現在我試圖從下面的循環獲取用戶標識。我希望能夠獲得用戶查找的名稱的用戶名(提交給用戶函數的名稱$ friend)。所以基本上我想能夠使用result ['userid']並能夠將其提交到數據庫中。

我在代碼中評論我無法獲得用於設置用戶標識的值。

<input type="hidden" name="userId" value="' . $result['userid'] . '" /> 

是否有某種方法來使用隱藏的輸入,或者只是沒有正確設置的值?

<?php 
include_once 'config.php'; 

class Friends{ 

function addFriend($userId) { 
    return $userId; //this is supposed to return the value of the user's id selected in the loop below via the if statements towards the bottom. 
} 

function findUsers($friend){ 
$search = mysql_query("SELECT * from users where username='$friend'"); 
if (mysql_num_rows($search) > 0){ 
// $this->addFriend($friend); 
$userLocation = mysql_query("select * from userinfo where username='$friend'"); 
     $locationResult = mysql_fetch_array($userLocation); 
     $locationResultArray = $locationResult['userlocation']; 
     $locationExplode = explode("~","$locationResultArray"); 
if (mysql_num_rows($search)) { 
    // Table column names 
    echo '<table><tr><td>Username</td><td>Location</td></tr>'; 
    while($result = mysql_fetch_array($search)) { 
    echo '<tr> 
    <td><a href="profile.php?userid=' . $result['userid'] . '">'. $result['username'] . '</a></td> 
    <td>' . $locationExplode[0] . ', ' . $locationExplode[1] . '</td> 
    <td> 
    <form method="post" name="friendRequest" action=""> 
    <input type="hidden" name="userId" value="' . $result['userid'] . '" /> 
    <input type="submit" name="addFriend" value="Add Friend" /> 
    </form> 
    </td></tr>'; 
    } 

    } 
    } 
} 
} 

$friends = new Friends(); 
    if (isset($_POST['userId'], $_POST['addFriend'])) { 
    echo "friend button pressed"; //this message is displayed 
    if ($friends->addFriend($_POST['userId'])) { 
      echo "userID set"; //this message is displayed 
      echo $_POST['userID']; //this is not displayed 
} else { 
// some error code here 
} 
} 


// Edit this to test here 
// $friends->findUsers('<username>'); 
?> 
+2

對於初學者來說,你的代碼在語法上是不正確的,它甚至在運行嗎? 告訴我們你遇到了什麼錯誤,然後你可能會得到一些幫助。 – wyqydsyq

+0

我更新了代碼以包含完整的東西。我以爲循環就夠了,不要猜測。我希望這可以讓它更清晰一些。 – user1104854

回答

3

這樣加的朋友是不正確的方法,因爲當你點擊「添加好友」按鈕,即會發送$_POST['addFriend'],然後在循環檢查會向所有用戶添加爲好友。

正確的代碼是在這裏:

<?php 
function addFriend($userId){ 
    // check is 'userId' exist, if not, then return 0; 
} 

if (isset($_POST['userId'], $_POST['addFriend'])) { 
    if (addFriend($_POST['userId'])) { 
    // some display code here 
    } else { 
    // some error code here 
    } 
} 
while($result = mysql_fetch_array($search)) { 
?> 
<tr><td> 
<form method="post" name="friendRequest" action=""> 
<input type="hidden" name="userId" value="<?php echo $result['userid']; ?>" /> 
<input type="submit" name="addFriend" value="Add Friend" /> 
</form> 
</td></tr> 
<?php } ?> 

EDIT1:

不能使用上面的代碼到一個函數。我修復了很多可以在代碼中看到的錯誤,但仍然看起來很奇怪。

我沒有得到你想要你的代碼做什麼,但我做到了這一點:

<?php 
function addFriend($userId) { 
    return 1; //using 1 for testing purposes 
} 

function findUsers($friend) { 
    $search = mysql_query('SELECT `userid`, `username`, `userlocation` FROM `users` JOIN `userinfo` ON `users`.`username` = `userinfo`.`username` WHERE `user`.`username` = ' . $friend); 
    if (mysql_num_rows($search)) { 
    // Table column names 
    echo '<table><tr><td>Username</td><td>Location</td></tr>'; 

    while($result = mysql_fetch_array($search)) { 
     $locationExplode = explode('~', $result['userlocation']); 
     echo '<tr> 
<td><a href="profile.php?userid=' . $result['userid'] . '">'. $result['username'] . '</a></td> 
<td>' . $locationExplode[0] . ', ' . $locationExplode[1] . '</td> 
<td> 
<form method="post" name="friendRequest" action=""> 
<input type="hidden" name="userId" value="' . $result['userid'] . '" /> 
<input type="submit" name="addFriend" value="Add Friend" /> 
</form> 
</td></tr>'; 
    } 
    } 
} 

if (isset($_POST['userId'], $_POST['addFriend'])) { 
    if (addFriend($_POST['userId'])) { 
    echo "test"; //I'm simply trying to get the input to work, can't get it to post. Just using this for a test. 
    } else { 
    // some error code here 
    } 
} 

// Edit this to test here 
// findUsers('<username>'); 
?> 

EDIT2:

那麼,你只需要把我的功能代碼寫入類,然後使用類之外的其他代碼,如下所示:

<?php 
include_once 'config.php'; 

class Friends{ 
    function addFriend($userId) { 
    return 1; //using 1 for testing purposes 
    } 

    function findUsers($friend) { 
    $search = mysql_query('SELECT `userid`, `username`, `userlocation` FROM `users` JOIN `userinfo` ON `users`.`username` = `userinfo`.`username` WHERE `user`.`username` = ' . $friend); 
    if (mysql_num_rows($search)) { 
     // Table column names 
     echo '<table><tr><td>Username</td><td>Location</td></tr>'; 

     while($result = mysql_fetch_array($search)) { 
     $locationExplode = explode('~', $result['userlocation']); 
     echo '<tr> 
<td><a href="profile.php?userid=' . $result['userid'] . '">'. $result['username'] . '</a></td> 
<td>' . $locationExplode[0] . ', ' . $locationExplode[1] . '</td> 
<td> 
<form method="post" name="friendRequest" action=""> 
<input type="hidden" name="userId" value="' . $result['userid'] . '" /> 
<input type="submit" name="addFriend" value="Add Friend" /> 
</form> 
</td></tr>'; 
     } 
    } 
    } 
} 

$friends = new Friends(); 
if (isset($_POST['userId'], $_POST['addFriend'])) { 
    if ($friends->addFriend($_POST['userId'])) { 
    echo "test"; 
    } else { 
    // some error code here 
    } 
} 

// Edit this to test here 
// $friends->findUsers('<username>'); 
?> 

EDIT3:

這是因爲該功能addFriend是不正確......你需要通過用戶ID值作爲參數,然後像這樣顯示的:

function addFriend($userId) { 
    return $userId; //this is supposed to return the value of the user's id selected in the loop below via the if statements towards the bottom. 
} 
+0

我明白你在做什麼,但我無法正常工作。我上面更新了我的代碼。你有任何建議或看到任何重大錯誤? – user1104854

+0

再次感謝您的幫助。 findUsers函數的全部重點是允許用戶搜索其他用戶。搜索返回用戶名(鏈接到他們的個人資料),位置和「添加朋友」按鈕。當用戶按下添加朋友時,它將它們添加爲朋友。我沒有編寫代碼將它們添加爲朋友,因爲我無法正確發佈它們。我想先解決發佈問題 – user1104854

+0

好的,對不起,我的MySQL語句中有一個錯誤,我修復了它並更新了答案。 –