gpt4 book ai didi

php - 搜索显示所有产品

转载 作者:行者123 更新时间:2023-11-29 23:19:24 25 4
gpt4 key购买 nike

您好,当我通过在 MYSQL 中插入的关键字搜索产品时,所有产品都会出现,请帮助我,这是搜索代码我按照评论中的方式进行了更正,但它直到不起作用

这是我的整个结果页面

<!DOCTYPE html>
<?php
include ("functions/functions.php");
?>
<html>
<head>
<title>eRiviera</title>
<meta charset='utf-8'>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="styles/style.css" media="all"/>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script src="js/menubarscript.js"></script>
</head>
<body>
<div class="main_wrapper">
<ul class="btn-circles">
<li><a href="#" class="round green">Login<span class="round">That is, if you already have an account.</span></a></li>
<li><a href="#" class="round red">Sign Up<span class="round">But only if you really, really want to.</span></a></li>
</ul>

<!--Header starts here-->
<div class="header_wrapper">
<!--Logo-->
<img id="logo" src="http://localhost/ecommerce/images/logo.png" width="500px" height="300px" alt="Logo" />
<!--Logo-->
</div>
<!--Header ends here-->


<!--Menu bar starts here-->
<div id='cssmenu'>
<ul>
<li class='active'><a href='index.php'>Home</a></li>
<li><a href='#'>Products</a></li>
<li><a href='#'>About</a></li>
<li><a href='#'>Contact</a></li>
<p style="float:right; margin-right:140px; margin-top:21px; color:red;">Welcome Guest!</p>
<li><a id="shopping_cart" style="margin:14px 0 0 0;left:750px; color:blue; font-size:12px;" href="cart.php">Shopping Cart</a></li>
</ul>
</div>

<form class="form-wrapper cf">
<input type="text" name="user_query" placeholder="Search here..." required>
<form method="get" action="results.php" enctype="multipart/form-data">
<button type="submit" name="search" value="Search">Search</button>
</form>
<ul id="cats">
<?php getCats(); ?>
</ul>

<!--Menu bar ends here-->

<!--Content wrapper starts here-->
<div class="content_wrapper">
<div id="content_area">
<div id="products_box">
<?php
if(isset$_GET['search'])) {

$search_query = $_GET['user_query'];
$get_pro = "select * from products where product_keywords like '%$search_query%'";

$run_pro = mysqli_query($con, $get_pro);

while($row_pro = mysqli_fetch_array($run_pro)) {

$pro_id = $row_pro['product_id'];
$pro_cat = $row_pro['product_cat'];
$pro_brand = $row_pro['product_brand'];
$pro_title = $row_pro ['product_title'] ;
$pro_price = $row_pro['product_price'];
$pro_image = $row_pro['product_image'];

echo "
<div id='single_product'>
<h3 id='product_title'>$pro_title</h3>
<img src='admin_area/product_images/$pro_image' width='180' height='200' />
<p><b> $ $pro_price <b></p>
<a id='details-button' href='details.php?pro_id=$pro_id'>Details</a>
<a href='index.php?pro_id=$pro_id'><button class='button'>Add to Cart</button></a>
</div>
";

}

}

?>
?>
</div>
</div>
</div>
<!--Content wrapper ends here-->


<div id="footer">
<h5 style="text-align:center; padding-top:30px;">&copy;2014 eRiviera All Rights Reserved</h5>
</div>
<!--Main wrapper ends here-->


</body>
</html>

最佳答案

您的表单结构不正确 - “user_query”字段位于表单之外,因此永远不会设置 $_GET['user_query']。尝试改变这个:

<form class="form-wrapper cf">
<input type="text" name="user_query" placeholder="Search here..." required>
<form method="get" action="results.php" enctype="multipart/form-data">
<button type="submit" name="search" value="Search">Search</button>
</form>

对于这样的事情:

<div class="form-wrapper cf">
<form method="get" action="results.php" enctype="multipart/form-data">
<input type="text" name="user_query" placeholder="Search here..." required>
<button type="submit" name="search" value="Search">Search</button>
</form>
</div>

此外,正如其他几个人所指出的,这很容易受到 SQL 注入(inject)的影响。这篇文章讨论了与您非常相似的场景:How can I prevent SQL injection in PHP?

我强烈建议您通过验证服务运行生成的代码,以便捕获 html 中的错误。请务必使用生成的 html(从浏览器中的“查看源代码”复制),而不仅仅是 php 文件中的代码,因为验证器无法理解 PHP。 WWW 联盟有一个很好的工具:http://validator.w3.org/#validate_by_input

关于php - 搜索显示所有产品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27460754/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com