【问题标题】:Search is showing all the products搜索显示所有产品
【发布时间】:2015-02-12 01:25:08
【问题描述】:

您好,当我从我在 MYSQL 中插入的关键字中搜索产品时,所有产品都出现了,请帮助我,这是搜索的代码 我在 cmets 中进行了更正,但直到无法正常工作

这是我的整个结果页面

<!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>

【问题讨论】:

  • 此脚本完全易受 sql 注入攻击。不要将其发布到互联网上。此外,该脚本有语法错误。
  • 不错的 SQL 注入。如果我将 x'; DROP DATABASE; -- 传递为 user_query 会发生什么?
  • 你能提供一个search_query的例子吗?
  • 您会返回所有结果,因为您的变量 $search_query 是空的,所以如果您转储查询,您会看到 ... product_keywords like '%%'
  • 你的代码错误太多了。

标签: php mysql search


【解决方案1】:

您的表单结构不正确 - “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 注入的影响。这篇文章讨论了一个与您非常相似的场景:How can I prevent SQL injection in PHP?

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多