PHP不显示无序列表

时间:2018-12-02 02:42:57

标签: php mysql

我有一个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);
?>

customerselect.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 个答案:

答案 0 :(得分:0)

由于user3783243提醒我仔细检查POST字段,所以我意识到我在POST字段中收到的是customerid而不是customer.firstname。因此,在getcustomerinfo.php中,应按以下方式编辑查询:...WHERE customer.customerid...而不是...WHERE customer.firstname...