Search is showing all the products

前端 未结 1 399
花落未央
花落未央 2021-01-29 15:55

Hello when i am searching a product from its keywords that i inserted in MYSQL all the products are appearing please help me this is the code of the search i corrected as in the

相关标签:
1条回答
  • 2021-01-29 16:36

    Your form is not properly structured - the "user_query" field is outside of the form so $_GET['user_query'] would never be set. Try changing this:

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

    To something like this:

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

    Also, as several others have noted, this is susceptible to SQL injection. This post discusses a scenario very similar to yours: How can I prevent SQL injection in PHP?

    I strongly suggest you run your generated code through a validation service in order to catch errors in your html. Be sure to use the generated html (copy from "view source" in browser), not just the code from your php file because the validator won't understand the PHP. The WWW Consortium has a good tool: http://validator.w3.org/#validate_by_input

    0 讨论(0)
提交回复
热议问题