以下のコードは機能し、結果が得られますが、結果が正しく表示されません $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);
3 列目が合計ポイントの場合は、次のようになります。*
を使用している場合、列名を知ることは不可能です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 />";
}
}