使用PHP在表中显示MySQL数据

时间:2016-05-19 05:01:35

标签: php mysql database phpmyadmin

我不知道为什么但是我似乎无法找到我的代码有什么问题,表单和结果页面在下面,每当​​我在表单字段中输入任何内容时,它应该会出现使用数据库中的所有数据。但它只显示了一行?

每当我在表单字段中输入内容时,我都会收到一些错误,

警告:mysqli_num_rows()要求参数1为mysqli_result,在第72行的/Applications/XAMPP/xamppfiles/htdocs/results.php中给出布尔值。

形式

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Wescott Washing Machines</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>

<body>
<h1>Wescott Washing Machines - Search</h1>

<p>Search our database of washing machines by supplying the following details.</p>

<form action="results.php" method="POST">

<p><label for="model">Model:</label><br>
<input type="text" name="model" id="model" size="35"></p>

<p><label for="supplier">Supplier:</label><br>
<select name="supplier" id="supplier">
<option value="0">Any</option>

<?php

    $link = mysqli_connect('xxxx','xxxx','xxxx','xxxx');

    $query = "SELECT suppliers.supplier_id, suppliers.supplier_name FROM suppliers;";

    $result = mysqli_query($link, $query);

    mysqli_close($link);

    while ($row = mysqli_fetch_array($result)) {

        $supplier = $row['supplier_name'];
        $supplier_id = $row['supplier_id'];

echo <<<END

    <option value="$supplier_id">$supplier</option>

END;

    }

?>

</select></p>

<p><label for="type">Type:</label><br>
<input type="radio" name="type" id=type1 value="1"><label for="type1"> Front Loader</label><br>
<input type="radio" name="type" id=type2 value="2"><label for="type2"> Top Loader</label></p>

<p><label for="capacity">Capacity (kilograms):</label><br>
<input type="text" name="capacity" id="capacity" size ="10"></p>

<p><label for="lowprice">Lowest Price:</label><br>
<label for="lowprice">$</label><input type="text" name="lowprice" id="lowprice" size="20"></p>

<p><label for="highprice">Highest Price:</label><br>
<label for="highprice">$</label><input type="text" name="highprice" id="highprice" size="20"></p>

<p><input type="submit" value="Submit Query">
<input type="reset" value="Reset"></p>

</form>
</body>
</html>

结果

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Wescott Washing Machines</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>

<body>
<h1>Wescott Washing Machines - Search Results</h1>

<?php

if ($_POST) {

    $model = $_POST['model'];
    $supplier = $_POST['supplier'];
    $capacity = $_POST['capacity'];
    $price = $_POST['highprice'] . $_POST['lowprice'];

    if (isset($_POST['type'])) {

        $type = $_POST['type'];

    }
    else {

        $type = "";

    }

    $model = trim($model);
    $capacity = trim($capacity);
    $price = trim($price);

    $link = mysqli_connect('xxxx','xxxx','xxxx','xxxx');

    $query = "SELECT machines.machine_id, machines.machine_model, machines.supplier_id, machines.machine_type, machines.machine_capacity, machines.machine_price, suppliers.supplier_name FROM machines, suppliers WHERE machines.supplier_id = suppliers.supplier_id ;";

    if ($model != "") {

        $query .= "AND machines.machine_model = '$model' ";

    }

    if ($supplier != 0) {

        $query .= "AND machines.supplier_id = $supplier ";

    }

    if ($capacity != "") {

        $query .= "AND machines.machine_capacity = '$capacity' ";

    }

    if ($price != "") {

        $query .= "AND machines.machine_price = '$price' ";

    }

    if ($type != "") {

        $query .= "AND machines.machine_type = '$type' ";

    }

    $result = mysqli_query($link, $query);

    $numberofrows = mysqli_num_rows($result);

    mysqli_close($link);

    if ($numberofrows == 0) {

        echo <<<END

        <p>There is no matching data in the database. Use the BACK button on your web browser to return to the previous page and try again</p>

END;

    }
    else {

        echo <<<END

        <p>Search Results are presented below.</p>
        <table>
        <tr>
            <th>Washing machine ID</th>
            <th>Model</th>
            <th>Supplier</th>
            <th>Type</th>
            <th>Capacity (kg)</th>
            <th>Price</th>
        </tr>

END;

        while ($row = mysqli_fetch_array($result)) {

            $machine_id = $row['machine_id'];
            $model = $row['machine_model'];
            $supplier = $row['supplier_name'];
            $type = $row['machine_type'];
            $capacity = $row['machine_capacity'];
            $price = number_format($row['machine_price'], 2);

        }

        echo <<<END

        <tr>
            <td>$machine_id</td>
            <td>$model</td>
            <td>$supplier</td>
            <td>$type</td>
            <td>$capacity</td>
            <td>$$price</td>
        <tr>  

END;

    }

    echo "</table>";

}
?>
</table>
</body>
</html>

3 个答案:

答案 0 :(得分:0)

从下面的查询中删除分号。

$query = "SELECT suppliers.supplier_id, suppliers.supplier_name FROM suppliers";

您的查询不正确。如果您需要使用两个表,那么您必须 JOIN

使用以下查询。

$query = "SELECT machines.machine_id, machines.machine_model, machines.supplier_id, machines.machine_type, machines.machine_capacity, machines.machine_price, suppliers.supplier_name FROM machines
          INNER JOIN suppliers ON machines.supplier_id = suppliers.supplier_id WHERE 1=1 ";

答案 1 :(得分:0)

你好我认为你的查询中有错误你输了分号;它可能会导致错误,因为当输入中存在某些东西时你会附加条件,所以这个分号内部查询会导致问题。

$ query =&#34; SELECT machines.machine_id,machines.machine_model,machines.supplier_id,machines.machine_type,machines.machine_capacity,machines.machine_price,suppliers.supplier_name FROM machines,suppliers WHERE machines.supplier_id = suppliers.supplier_id ;&#34 ;;

如果您的问题将得到解决,请用我的下方替换您的查询。

$ query =&#34; SELECT machines.machine_id,machines.machine_model,machines.supplier_id,machines.machine_type,machines.machine_capacity,machines.machine_price,suppliers.supplier_name FROM machines,suppliers WHERE machines.supplier_id = suppliers.supplier_id在哪里1 = 1&#34 ;;

由于这个分号,当输入中的某些内容你附加了一些条件,所以它会导致错误。你需要添加WHERE 1 = 1,因为当你有输入的东西时你将直接添加,所以在此之前{ {1}}是必需的

答案 2 :(得分:0)

SQL语句末尾的分号中的一个问题。这不应该是SQL文本的一部分。删除它。

$query = "SELECT suppliers.supplier_id, suppliers.supplier_name FROM suppliers;";
                                          // remove this semicolon here ---   ^

并且不要关闭与MySQL数据库的连接,直到 之后从查询返回的结果集中获取行,并在完成执行MySQL语句之后。移动这一行:

mysqli_close($link);
在 while循环之后

,在您从结果集中获取行之后。

此外,在从语句句柄获取行之前,请确保语句句柄是有效的mysqli_result_set,而不是布尔值FALSE。