du2229 2014-11-19 02:36
浏览 82
已采纳

通过PHP单击HTML按钮发送SQL命令

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:

<table border="1">
   <tr>
      <td>Last name</td>
      <td>First Name</td>
      <td>Job Title</td>
      <td>City</td>
      <td>Client List</td>
   </tr>
   <tr>
      <td>Bondur</td>
      <td>Gerard</td>
      <td>Sale Manager (EMEA)</td>
      <td>Paris</td>
      <td>
         <form method="POST" action="empLookup.php">
         <input type="submit" name="empLookup" value="Look up clients"
      </td>
   </tr>
</table>

By clicking on the button I would run a MySQL command like 'SELECT clients FROM blah WHERE employeeNumber = ?'

I don't have a problem with any of this except passing the value from the button to the PHP script.

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.

<?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 '<h1>Our Associates by City</h1>';

      Type in a Name to view Years</a><br>';
//create the form
echo 'Please select a year: <br>';
echo '<form action="index.php" method="post">';
echo '<select name= "city">';

foreach($conn->query($sql) as $row)
{
    //each option in the drop down menu is each and every year
    //brought up by the query
    echo '<option value ="'. $row['city'].' ">'. $row['city']. '</option>';
} //end of foreach
echo '</select>'; //end drop down menu

//now to create the submit button
echo '<br><input type="submit" name="submit" value="List"><br>';
echo '</form>'; //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 '<br><br>';
    $sql2 = "SELECT employeeNumber,lastName,firstName,jobTitle,city
             FROM Employees,Offices
             WHERE Employees.officeCode = Offices.officeCode AND city = ?";
    $stmt = $conn->prepare($sql2);
    $stmt->execute(array($flit));
    $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

    echo 'Contact any of our local staff: <br>';

    //create a table of employees
    echo '<table border="1"><tr><td>Last name</td><td>First Name</td>';
    echo '<td>Job Title</td><td>City</td></tr>';

    //time to populate the table, this loop runs for each entry
    foreach($rows as $r)
    {
        echo '<tr><td>'.$r[lastName].'</td><td>'.$r[firstName].'</td><td>';
        echo $r[jobTitle].'</td><td>'.$r[city].'</td><td>';
        echo '<form method="POST" action="empLookup.php">';
        //now to make the button which will search the employee's client list
        echo '<input type="submit" name="empLookup" value="Look up clients"</td></tr>';
    } //end foreach
    echo '</table>';
 } //end if server request post thing
?>
  • 写回答

2条回答 默认 最新

  • doushouj966020 2014-11-19 03:05
    关注

    I does not completely understood your exact requirement but I think you want employee number into your button if this is your requirement then you can simply check this code

    `echo '<input type="submit" name="empLookup" value="'.$r['emp_id_from_database'].'"</td></tr>';`
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘