共有3个页面,其中一个下拉列表正在运行,但我无法对这两个页面进行过滤。这可能是一个语法错误,因为我是PHP的初学者。我确实试图在很多方面改变search.php,但它似乎没有帮助。任何帮助表示赞赏。
Products.html
<form action="search.php" method='post'>
<select name="PC">
<option value="HP PC"> HP PC</option>
<option value="Samsung Laptop">Samsung Laptop</option>
<option value="Sony Phone">Sony phone</option>
<option value="Dell PC">Dell PC</option>
</select>
<select price="price">
<option value="599.00"> 599</option>
<option value="300.00"> 300</option>
</select>
<input type="submit" id= "searchBtn" name='name' price='price' value= "Go!"/>
</form>
Search.php
<?php
include "main.php";
if(isset($_POST['name'])){
$name=$_POST['PC'];
search($name);
}
if(isset($_POST['price'])){
$price=$_POST['price'];
search($price);}
?>
Main.php
function search($name,$price){
//Connect to the database
$conn = connect();
$query = "SELECT * FROM t_product WHERE name LIKE '%$name%' AND price LIKE '%$price%' ";
$results = mysqli_query($conn, $query);
while($row = mysqli_fetch_array($results) ){
echo "<tr>
<td>$row[name]</td>
<td>$row[price]</td>
<td><img src='$row[imagepath]' width='150' height='100' /></td>
</form>
</td>
</tr>";
}
echo "</table>";
//close the table tags
}
谢谢。
答案 0 :(得分:1)
<强> products.html放在强>
<form action="search.php" method='post'>
<select name="PC">
<option value="HP PC"> HP PC</option>
<option value="Samsung Laptop">Samsung Laptop</option>
<option value="Sony Phone">Sony phone</option>
<option value="Dell PC">Dell PC</option>
</select>
<select name="price">
<option value="599.00"> 599</option>
<option value="300.00"> 300</option>
</select>
<input type="submit" id= "searchBtn" name='name' price='price' value= "Go!"/>
</form>
<强>的search.php 强>
<?php
if(isset( $_POST[ 'name' ] ) ) {
$name=$_POST[ 'PC' ];
}
if(isset( $_POST[ 'price' ] ) ) {
$price=$_POST[ 'price' ];
}
$conn = connect();
$query = "SELECT * FROM t_product WHERE name LIKE '%$name%' AND price LIKE '%$price%' ";
$results = mysqli_query( $conn, $query );
echo '<table>';
while( $row = mysqli_fetch_array( $results ) ) {
echo "<tr>
<td>" .$row[name]. "</td>
<td>" .$row[price]. "</td>
<td><img src='".$row[imagepath]."' width='150' height='100' /></td>
</tr>";
}
echo '</table>';
?>
试试这个。
答案 1 :(得分:0)
$nav_query = $Navigations->find('path', ['for' => 33]);
这很可能是您的查询无效的原因。尝试将%$ name%和%$ price%直接作为$ name和$ price。
无论如何,要查看MySql查询的错误,请尝试以下方法:
echo(&#34;错误描述:&#34; .mysqli_error($ con));
然后,您将确切地看到错误是什么,并且您可以修复查询
但首先,试试
$query = "SELECT * FROM t_product WHERE name LIKE '%$name%' AND price LIKE '%$price%' ";
确保相应地定义变量。