我有一个php程序,它应该在从下拉菜单中选择客户名称后打印客户购买历史记录。但是,在选择客户之后,应该显示的无序列表为空。
这是我的密码
<!DOCTYPE html>
<html>
<body>
<script src="customerselect.js"></script>
<?php
include "connecttodb.php";
include "getcustomer.php";
?>
<h1>Customers</h1>
<br>
<a href="customers.php">Customers</a>
<a href="products.php">Products</a>
<br>
<hr>
<hr>
Select the customer whom you'd like to see what items they've purchased:
<form action="" method = "post">
<select name="pickacustomer" id="pickacustomer">
<option value="1">Select Here</option>
<?php
include "getcustomername.php";
?>
</select>
</form>
<hr>
<?php
if (isset($_POST['pickacustomer'])){
include "connecttodb.php";
include "getcustomerinfo.php"; #This is the line that I expect to print my customer purchase history
}
?>
<hr>
<br>
<br>
<h2>List of all Customers</h2>
</body>
</html>
getcustomerinfo.php文件
<?php
$connection = mysqli_connect("localhost", "root", "xxx", "xxx") or die("Not connected");
$whichCustomer = $_POST["pickacustomer"];
$query = "
SELECT c.firstname
, c.lastname
, p.description
, u.quantitybought
FROM purchased u
JOIN customer c
ON u.customerid = c.customerid
JOIN products p
ON u.productid = p.productid
WHERE c.firstname = ". $whichCustomer.";";
$result = mysqli_query($connection, $query);
if (!$result) {
die("databases query failed - getcustomerinfo.php.");
}
echo "<ul>";
while ($row = mysqli_fetch_assoc($result)){
echo "<li>" . $row["firstname"] . $row["lastname"]. $row["description"]. $row["quantitybought"]. "</li>";
}
echo "</ul>";
mysqli_free_result($result);
?>
客户选择.js
window.onload=function(){
prepareListener();
}
function prepareListener(){
var droppy;
droppy = document.getElementById("pickacustomer");
droppy.addEventListener("change",getCustomer);
}
function getCustomer(){
this.form.submit();
}
我知道getcustomerinfo.php中的查询是有效的,因为如果我输入一个实际的客户名称,它将显示它。但是,当我尝试动态地为它指定一个名称时,程序无法显示正确的信息。请原谅这段代码写得太差了,因为我刚刚开始学习php。
1条答案
按热度按时间dauxcl2d1#
感谢user3783243提醒我仔细检查post字段,我意识到我收到的是customerid而不是post字段中的customer.firstname。因此,在getcustomerinfo.php中,应该像这样编辑查询:
...WHERE customer.customerid...
而不是...WHERE customer.firstname...