編輯 - 我解決了我的「添加朋友」按鈕問題,現在我試圖從下面的循環獲取用戶標識。我希望能夠獲得用戶查找的名稱的用戶名(提交給用戶函數的名稱$ 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>');
?>
對於初學者來說,你的代碼在語法上是不正確的,它甚至在運行嗎? 告訴我們你遇到了什麼錯誤,然後你可能會得到一些幫助。 – wyqydsyq
我更新了代碼以包含完整的東西。我以爲循環就夠了,不要猜測。我希望這可以讓它更清晰一些。 – user1104854