Is it possible to assign a button to a dynamic variable; which changes for each row extracted from the query in a while loop?


I have a query that produces a table using a while loop for each row it finds. However my goal is to have each row with an add button that when pressed takes two variables and INSERTS a new row in another table. I believe the reason my code isn't working to be down to the system not being able to identify which add button belongs to which row. I have tried assigning the UserID of each row to the button id but it still fails to INSERT. (It produces no errors).

Any suggests as to how I can achieve my goal?


<div class="RightBody">
  <form name="search_form" method="post" action="" >
    <input type="text" name="search_box" value="" placeholder="Search for skills or a user." />
    <input type="submit" name="search" value="Search" />
  <table width="100%" cellpadding="5" cellspacing="5" >
        <p><strong>Search Results:</strong></p>
          <td><strong>First Name</strong></td>
          <td><strong>Last Name</strong></td>
          <td><strong>Job Role</strong></td>
              $AddUserID = $search_rs['UserID'];
              $AddProjectID = $Project;

              $sql = $con->query("INSERT INTO userprojects (UserID, ProjectID) VALUES  ('$AddUserID', '$AddProjectID')") or die(mysqli_error($con));
                <input type="submit" name="adduser" value="Add"  id="<?php echo htmlspecialchars($search_rs['UserID']); ?>"/>
              <td><?php echo $search_rs['Fname']; ?></td>
              <td><?php echo $search_rs['Lname']; ?></td>
              <td><?php echo $search_rs['JobRole']; ?></td>
              <td><?php echo $search_rs['Description']; ?></td>
          while ($search_rs=mysqli_fetch_assoc($search_query));
        echo "No results found";


$value = isset($_POST['search_box']) ? $_POST['search_box'] : '';

$search_sql = "SELECT * FROM User INNER JOIN UserSkills ON User.UserId = UserSkills.UserId JOIN Skills ON UserSkills.SkillId = Skills.SkillId WHERE Description LIKE '%".$value."%' OR FName LIKE '%".$value."%' OR LName LIKE '%".$value."%' OR JobRole LIKE '%".$value."%' GROUP BY Description" or die(mysqli_error($con));

$search_query = mysqli_query($con, $search_sql);
if(mysqli_num_rows($search_query) !=0) {
  $search_rs = mysqli_fetch_assoc($search_query);

You need to specify your action attribute in your <form>.

Also, htmlspecialchars(value) will return a string, not output it, so instead of using:

<?php htmlspecialchars($search_rs['UserID']);

you need to use

<?php echo htmlspecialchars($search_rs['UserID']);