title: Sending a SQL command with one click of an HTML button through PHP
body: <p>So, I have a basic PHP site that brings up a list of salespeople from a MySQL server when a selection from a drop-down box is submitted. I've set up a button to appear next to each result, and I want a php script to run when the button is clicked using MySQL data from that specific result. Everything works except the button that runs the second MySQL query. Here's an example of the table after the first query:</p>
  
  <pre><code>&lt;table border="1"&gt;
     &lt;tr&gt;
        &lt;td&gt;Last name&lt;/td&gt;
        &lt;td&gt;First Name&lt;/td&gt;
        &lt;td&gt;Job Title&lt;/td&gt;
        &lt;td&gt;City&lt;/td&gt;
        &lt;td&gt;Client List&lt;/td&gt;
     &lt;/tr&gt;
     &lt;tr&gt;
        &lt;td&gt;Bondur&lt;/td&gt;
        &lt;td&gt;Gerard&lt;/td&gt;
        &lt;td&gt;Sale Manager (EMEA)&lt;/td&gt;
        &lt;td&gt;Paris&lt;/td&gt;
        &lt;td&gt;
           &lt;form method="POST" action="empLookup.php"&gt;
           &lt;input type="submit" name="empLookup" value="Look up clients"
        &lt;/td&gt;
     &lt;/tr&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>By clicking on the button I would run a MySQL command like 'SELECT clients FROM blah WHERE employeeNumber = ?'</p>
  
  <p>I don't have a problem with any of this except passing the value from the button to the PHP script.</p>
  
  <p>This is what my PHP code looks like for handling the form submission and display of results. The button(s) in question are in the HTML table in the foreach loop.</p>
  
  <pre><code>&lt;?php #this is the default php file for looking up Employees
  
  $page_title = 'Our Associates by City';
  require ('./pdoConn.php');
  $sql = "SELECT DISTINCT city from Offices";
  
  echo '&lt;h1&gt;Our Associates by City&lt;/h1&gt;';
  
        Type in a Name to view Years&lt;/a&gt;&lt;br&gt;';
  //create the form
  echo 'Please select a year: &lt;br&gt;';
  echo '&lt;form action="index.php" method="post"&gt;';
  echo '&lt;select name= "city"&gt;';
  
  foreach($conn-&gt;query($sql) as $row)
  {
      //each option in the drop down menu is each and every year
      //brought up by the query
      echo '&lt;option value ="'. $row['city'].' "&gt;'. $row['city']. '&lt;/option&gt;';
  } //end of foreach
  echo '&lt;/select&gt;'; //end drop down menu
  
  //now to create the submit button
  echo '&lt;br&gt;&lt;input type="submit" name="submit" value="List"&gt;&lt;br&gt;';
  echo '&lt;/form&gt;'; //end of form
  
  //This if statement runs when the submit button is clicked
  if ($_SERVER[REQUEST_METHOD] == 'POST')
  {
      $flit = $_POST[city]; //the city variable from the HTML form will be used
      echo '&lt;br&gt;&lt;br&gt;';
      $sql2 = "SELECT employeeNumber,lastName,firstName,jobTitle,city
               FROM Employees,Offices
               WHERE Employees.officeCode = Offices.officeCode AND city = ?";
      $stmt = $conn-&gt;prepare($sql2);
      $stmt-&gt;execute(array($flit));
      $rows = $stmt-&gt;fetchAll(PDO::FETCH_ASSOC);
  
      echo 'Contact any of our local staff: &lt;br&gt;';
  
      //create a table of employees
      echo '&lt;table border="1"&gt;&lt;tr&gt;&lt;td&gt;Last name&lt;/td&gt;&lt;td&gt;First Name&lt;/td&gt;';
      echo '&lt;td&gt;Job Title&lt;/td&gt;&lt;td&gt;City&lt;/td&gt;&lt;/tr&gt;';
  
      //time to populate the table, this loop runs for each entry
      foreach($rows as $r)
      {
          echo '&lt;tr&gt;&lt;td&gt;'.$r[lastName].'&lt;/td&gt;&lt;td&gt;'.$r[firstName].'&lt;/td&gt;&lt;td&gt;';
          echo $r[jobTitle].'&lt;/td&gt;&lt;td&gt;'.$r[city].'&lt;/td&gt;&lt;td&gt;';
          echo '&lt;form method="POST" action="empLookup.php"&gt;';
          //now to make the button which will search the employee's client list
          echo '&lt;input type="submit" name="empLookup" value="Look up clients"&lt;/td&gt;&lt;/tr&gt;';
      } //end foreach
      echo '&lt;/table&gt;';
   } //end if server request post thing
  ?&gt;
  </code></pre>