在php中搜索按钮没有结果错误消息



我遇到了麻烦,我应该把我的echo 'No Result Found';在代码代码我试着把它放在最后一个else语句一起在代码调试,但它不起作用。请帮助我应该把我的没有结果发现。提前谢谢。

if(!empty($_POST['search'])){
if($result = $db->query("SELECT * FROM product WHERE setname like '%".$_POST['search']."%' OR category like '%".$_POST['search']."%' ")) {
while($row = $result->fetch_assoc())    {
echo '<div class="col-sm-3">';
    echo '<form method="POST" action="buynow.php" enctype="multipart/form-data"> ';
        echo '<input type="hidden" name="productid" value="'.$row['id'].'">';
        echo '<img class="thumbnail img-responsive" src="data:image;base64,'.$row['image'].'  " >'; 
        echo '<p>Name :  ',$row['setname'], '</span></p>';
        echo '<p>Price :  ', $row['price'], '</span></p>';
        echo '<p>Bonus :  <span class="label label-info" style="font-size:16px;">', $row['status'], '</span></p>';
        echo '<p>Price Now :  ', $row['pricesale'], '</span></p>';
        echo '<p>Product Detail: ', $row['productdesc'] ,'</p>';
        echo '<button class="btn btn-danger btn-lg btn-block" type="submit" name="submit">Buy Now</button>';
        echo '</form>'; 
    echo '<br></div>';
}
echo '</div>';
echo '</div>';
}else{
//code for debugging query      
die($db->error);
}
}

您需要从查询中计算num_rows的数量。如果没有找到行,则显示没有找到结果

  if (!empty($_POST['search'])) {
        if ($result = $db->query("SELECT * FROM product WHERE setname like '%" . $_POST['search'] . "%' OR category like '%" . $_POST['search'] . "%' ")) {
            $row = $result->num_rows;
            if ($row > 0) {
                while ($row = $result->fetch_assoc()) {
                    echo '<div class="col-sm-3">';
                    echo '<form method="POST" action="buynow.php" enctype="multipart/form-data"> ';
                    echo '<input type="hidden" name="productid" value="' . $row['id'] . '">';
                    echo '<img class="thumbnail img-responsive" src="data:image;base64,' . $row['image'] . '  " >';
                    echo '<p>Name :  ', $row['setname'], '</span></p>';
                    echo '<p>Price :  ', $row['price'], '</span></p>';
                    echo '<p>Bonus :  <span class="label label-info" style="font-size:16px;">', $row['status'], '</span></p>';
                    echo '<p>Price Now :  ', $row['pricesale'], '</span></p>';
                    echo '<p>Product Detail: ', $row['productdesc'], '</p>';
                    echo '<button class="btn btn-danger btn-lg btn-block" type="submit" name="submit">Buy Now</button>';
                    echo '</form>';
                    echo '<br></div>';
                }
            } else {
                echo "No result Forund";
            }
        } else {
//code for debugging query      
            die($db->error);
        }
    }
    echo '</div>';
    echo '</div>';

您可以使用$result->num_rowsmysqli_num_rows($result)

if ($result->num_rows == 0) {
    echo 'No Result Found';
}
else {
    // Do your while
}

请使用准备好的语句

  1. 使用预处理语句
  2. 使用PDO,而不是mysqli。
  3. 模板使用。

所以是

$sql = "SELECT * FROM product WHERE setname like :search OR category like :search";
$stmt = $pdo->prepare($sql);
$stmt->execute(array('search' => '%'.$_POST['search'].'%'));
$data = $stmt->fetchAll();
?>
<? foreach($data as $row): ?>
<div class="col-sm-3">
    <form method="POST" action="buynow.php" enctype="multipart/form-data">
        <input type="hidden" name="productid" value="<?=$row['id']?>">
        <img class="thumbnail img-responsive" src="data:image;base64,<=$row['image']?>"> 
        <p>Name :  <?=$row['setname']?></span></p>
        <p>Price :  <?=$row['price']?></span></p>
        <p>Bonus :  <span class="label label-info" style="font-size:16px;">
            <?=$row['status']?>
        </span></p>
        <p>Price Now :  <?=$row['pricesale']?></span></p>
        <p>Product Detail: <?=$row['productdesc']?></p>
        <button class="btn btn-danger btn-lg btn-block" type="submit" name="submit">Buy Now</button>
    </form>
    <br>
</div>
<? endforeach ?>
<? if (!$data): ?>
    No Result Found
<? endif ?>

相关内容

  • 没有找到相关文章

最新更新