所以,我有一个基本的PHP站点,当从提交的下拉框中选择时,它会从MySQL服务器中显示销售人员列表。我已经设置了一个按钮显示在每个结果旁边,并且我希望在使用来自该特定结果的MySQL数据单击按钮时运行php脚本。除了运行第二个MySQL查询的按钮外,一切正常。以下是第一个查询后的表格示例:
<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>
通过点击按钮,我将运行一个MySQL命令,如'SELECT clients FROM blah WHERE employeeNumber =?'
除了将值从按钮传递给PHP脚本之外,我没有遇到任何问题。
这是我的PHP代码用于处理表单提交和结果显示的样子。有问题的按钮位于foreach循环的HTML表格中。
<?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
?>
答案 0 :(得分:0)
从您的HTML代码中,您的表单看起来是空的。
您需要将数据添加到html表单中。如果您想避免用户看到您可以使用字段。就像在评论中说的那样,使用$ variableName而不是?在您的查询中。不要忘记使用\“$ variableName \”来避免mysql注入。
我对你的代码进行了第二次阅读:在为高效的公司网站编写程序之前,你应该完整地阅读一本php书。您的代码中存在初学者错误。一些初学者的错误导致网站不安全。我希望这看起来不是冒犯,而是像建议一样。
答案 1 :(得分:0)
我并不完全了解您的确切要求,但我认为如果这是您的要求,您希望员工编号进入您的按钮,那么您只需查看此代码
`echo '<input type="submit" name="empLookup" value="'.$r['emp_id_from_database'].'"</td></tr>';`