PHP在循环中使用$ _POST时出现问题

时间:2012-01-19 02:52:06

标签: php mysql post loops submit

编辑 - 我解决了我的“添加好友”按钮问题,现在我正试图从下面的循环中获取用户ID。我希望能够获得用户查找的名称的用户标识(提交给findUsers函数的名称,$ friend)。所以基本上我希望能够使用结果['userid']并能够将其提交到数据库中。

我在代码中注释了我无法获取要设置的用户ID的值。

<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>');
?>

1 个答案:

答案 0 :(得分: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:

您无法将上述代码用于函数中。我修复了很多我可以在你的代码中看到的bug,但看起来仍然很奇怪。

我不知道你想用你的代码做什么,但我做了这个:

<?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.
}