Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7064659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:51:41+00:00 2026-05-28T04:51:41+00:00

edit – I solved my add friend button issue, now I’m trying to get

  • 0

edit – I solved my “add friend” button issue, now I’m trying to get the userid from the loop below. I want to be able to get the userid of the name that the user looks up (the name that gets submitted to findUsers function, $friend). So basically I want to be able to use result[‘userid’] and be able to submit that into a database.

I commented in the code where I’m having trouble getting the value for the userid to set.

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

Is there a certain way to use hidden inputs, or is the value just not being set correctly?

<?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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T04:51:41+00:00Added an answer on May 28, 2026 at 4:51 am

    That way to add friend is incorrect way, because when you click the “Add friend” button, that will send a $_POST['addFriend'] and then in the loop the check are going to add all users as friend.

    The correct code is here:

    <?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:

    You can’t use the code above into a function. I fixed a lot of bug that I can see in your code, but still look strange.

    I don’t get what you want to do with your code, but I made this:

    <?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:

    Well, you just need to put my functions code into the class and then use the other code outside the class, like this:

    <?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:

    That’s because the function addFriend is incorrect… You need to pass the user ID value as argument and then display it like this:

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Edit : Solved, there was a trigger with a loop on the table (read
edit #2: Question solved halfways. Look below As a follow-up question, does anyone know
Edit: I have solved this by myself. See my answer below I have set
Edit: From another question I provided an answer that has links to a lot
EDIT #2 - I'm done the previous state part. Now I need to my
EDIT: SORRY! Turns out that I'm an idiot. The exception was being thrown from
EDIT: The cause of the errors (see below): my default primary key in doctrine_pi.php
Edit: The below question was answered by this . I have a new updated
Edit: I'm looking for solution for this question now also with other programming languages.
EDIT: Changed as I have a different issue with the same code 2nd Edit:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.