通过PHP单击HTML按钮发送SQL命令(Sending a SQL command with one click of an HTML button through PHP)

编程入门 行业动态 更新时间:2024-10-05 07:23:21
通过PHP单击HTML按钮发送SQL命令(Sending a SQL command with one click of an HTML button through PHP)

所以,我有一个基本的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 ?>

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

最满意答案

我不完全理解您的确切要求,但我认为如果这是您的要求,您希望员工编号进入您的按钮,那么您只需检查此代码

`echo '<input type="submit" name="empLookup" value="'.$r['emp_id_from_database'].'"</td></tr>';`

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>';`

更多推荐

本文发布于:2023-08-07 10:55:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464108.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单击   按钮   命令   HTML   PHP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!