下面的代码工作并给你结果,但它没有正确显示结果如何修改 $search_output?
<?php
$search_output = "";
if (isset($_POST['searchquery']) && $_POST['searchquery'] != "") {
$searchquery = preg_replace('/[^a-zA-Z0-9_ %\[\]\/\.\(\)%&-]/s', '', $_POST['searchquery']);
if ($_POST['filter1'] == "Products") {
require_once ("storescripts/connect_to_mysqli.php");
//syntax error string not quoted properly
$stmt = $myConnection->prepare('SELECT * FROM products WHERE product_name LIKE ? OR details LIKE ? OR category LIKE ? OR subcategory LIKE ? OR price LIKE ?');
$param = "%$searchquery%";
$stmt->bind_param('sssss', $param , $param , $param , $param , $param);
$stmt->execute();
$stmt->bind_result($id, $product_name, $price);
while ($stmt->fetch()) {
printf("%s %s %s\n", $id, $product_name, $price);
$search_output .= "
<li><div class='product'>
<a href='product.php?id=$id' class='info'>
<span class='holder'>
<img src='inventory_images/$id.jpg' alt='$product_name' />
<span class='book-name'>$product_name</span>
</a>
<a href='product.php?id=$id' class='buy-btn'>RM<span class='price'>$price</span></a>
</div>
</li>
";
}//While loop was not closed
} else {
$search_output = "<hr />0 results for <strong>$searchquery</strong><hr />";
}
}
?>
你有 2 个语法错误。编辑以修复绑定语句并更改 sql
<?php
$search_output = "";
if (isset($_POST['searchquery']) && $_POST['searchquery'] != "") {
$searchquery = preg_replace('/[^a-zA-Z0-9_ %\[\]\/\.\(\)%&-]/s', '', $_POST['searchquery']);
if ($_POST['filter1'] == "Products") {
require_once ("storescripts/connect_to_mysqli.php");
//syntax error string not quoted properly
$stmt = $myConnection->prepare('SELECT * FROM products WHERE product_name LIKE ? OR details LIKE ? OR category LIKE ? OR subcategory LIKE ? OR price LIKE ?');
$param = "%$searchquery%";
$stmt->bind_param('sssss', $param , $param , $param , $param , $param);
$stmt->execute();
$stmt->bind_result($product_name, $price);
while ($stmt->fetch()) {
printf("%s %s\n", $product_name, $price, $totalpoints);
$search_output .= "
<li><div class='product'>
<a href='product.php?id=$id' class='info'>
<span class='holder'>
<img src='inventory_images/$id.jpg' alt='$product_name' />
<span class='book-name'>$product_name</span>
</a>
<a href='product.php?id=$id' class='buy-btn'>RM<span class='price'>$price</span></a>
</div>
</li>
";
}//While loop was not closed
} else {
$search_output = "<hr />0 results for <strong>$searchquery</strong><hr />";
}
}
?>
您还需要绑定所有 5 个参数更改以下行。
$stmt->bind_param('s', '%$searchquery%');
编辑:
$stmt->bind_result($product_name, $price);
如果第三列是总分,则应如下所示。*
使用时不可能知道列名select
$stmt->bind_result($product_name, $price, $totalpoints);
和
printf("%s %s\n", $product_name, $price, $totalpoints);
可
printf("%s %s %s\n", $product_name, $price, $totalpoints);
要不就
echo "$product_name $price $totalpoints\n";
获取执行语句后的总使用量。
$count = $stmt->num_rows;
编辑 2
检查这是否也有效。
$search_output = "";
if (isset($_POST['searchquery']) && $_POST['searchquery'] != "") {
$searchquery = preg_replace('/[^a-zA-Z0-9_ %\[\]\/\.\(\)%&-]/s', '', $_POST['searchquery']);
if ($_POST['filter1'] == "Products") {
require_once ("storescripts/connect_to_mysqli.php");
$stmt = $myConnection->prepare('SELECT id, product_name, price FROM products WHERE product_name LIKE ? OR details LIKE ? OR category LIKE ? OR subcategory LIKE ? OR price LIKE ?');
$param = "%$searchquery%";
$stmt->bind_param('sssss', $param, $param, $param, $param, $param);
$stmt->execute();
/* store result */
$stmt->store_result();
/* get the row count */
$count = $stmt->num_rows;
if ($count >= 1) {
$search_output = "<hr />$count results for <strong>$searchquery</strong><hr />";
$stmt->bind_result($id, $product_name, $price);
while ($stmt->fetch()) {
printf("%s %s %s\n", $id, $product_name, $price);
$search_output .= "
<li><div class='product'>
<a href='product.php?id=$id' class='info'>
<span class='holder'>
<img src='inventory_images/$id.jpg' alt='$product_name' />
<span class='book-name'>$product_name</span>
</a>
<a href='product.php?id=$id' class='buy-btn'>RM<span class='price'>$price</span></a>
</div>
</li>
";
}
} else {
$search_output = "<hr />0 results for <strong>$searchquery</strong><hr />";
}
} else {
$search_output = "<hr />0 results for <strong>$searchquery</strong><hr />";
}
}