1

Hi here i am trying to get values from database using query in my php page. i am getting values from database to dropdown in loop using php.

here is the html of the output

<li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Products <span class="caret"></span></a>
          <ul class="dropdown-menu">            
            <li><a href='product-by-category.php?Aviation'>Aviation</a></li><li><a href='product-by-category.php?beauty & personal care'>beauty & personal care</a></li><li><a href='product-by-category.php?Documentary'>Documentary</a></li><li><a href='product-by-category.php?gaming'>gaming</a></li><li><a href='product-by-category.php?health and fitness'>health and fitness</a></li><li><a href='product-by-category.php?health care'>health care</a></li><li><a href='product-by-category.php?hobbies '>hobbies </a></li><li><a href='product-by-category.php?home '>home </a></li><li><a href='product-by-category.php?misc'>misc</a></li><li><a href='product-by-category.php?mobile accessories '>mobile accessories </a></li><li><a href='product-by-category.php?mobiles'>mobiles</a></li><li><a href='product-by-category.php?Music'>Music</a></li><li><a href='product-by-category.php?office '>office </a></li><li><a href='product-by-category.php?photography'>photography</a></li><li><a href='product-by-category.php?sports'>sports</a></li><li><a href='product-by-category.php?tool & hardware '>tool & hardware </a></li>          </ul>
        </li>

these values will redirect to page called product-by-category.php with parameter in URL. the parameter will be used to fetch the values from database

when i pass the parameter for e.x sports , i am able to get the results but when i pass tool & hardware as parameter i am not able to get the results because of space and i see tool%20&%20hardware in URL as parameter.

how can i solve this

here is query i am using

"SELECT product_id, product_name, product_price, product_image_URL FROM product_list WHERE product_publish_status='0' and product_category ='$product_category'";

here $product_category is parameter that it will get from url

$product_category = $_SERVER['QUERY_STRING'];
9
  • The query doesn't use the URL parameter, so how can it make a difference? Commented Aug 27, 2015 at 7:24
  • Where is your where clause in your query to compare your value?? Commented Aug 27, 2015 at 7:24
  • Use a category number or stricter identifier. Commented Aug 27, 2015 at 7:25
  • Your URL parameter should be in the form name=value. You don't have a name for the parameter. Commented Aug 27, 2015 at 7:25
  • 1
    have you try with urldecode() Commented Aug 27, 2015 at 7:33

2 Answers 2

3

Since you're accessing $_SERVER['QUERY_STRING'] directly, you need to do URL decoding:

$product_category = urldecode($_SERVER['QUERY_STRING']);

But it would be better if you used normal query parameters, e.g.

product-by-category.php?category=Aviation

Then you could access it with:

$product_category = $_GET['category'];

Parameters are automatically decoded when they're put into $_GET.

Also, the code that creates the URLs should use urlencode. Otherwise you'll have problems if there are categories with some special characters like % or +. It should be like:

echo "<li><a href='product-by-category.php?" . urlencode($row['category']) . "'>Aviation</a></li>";
Sign up to request clarification or add additional context in comments.

Comments

1

You should use urldecode to decode the encoded string in your query:

Here the link should be:

echo "<li><a href='product-by-category.php?category=" . urlencode(Aviation) . "'>Aviation</a></li>";

You can access it using $_GET php global variable.

$product_category = $_GET['category'];
$product_category = mysqli_real_escape_string($product_category); // Addition

"SELECT product_id, product_name, product_price, product_image_URL FROM product_list WHERE product_publish_status='0' and product_category ='".$product_category."'";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.