Reputation: 7257
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'];
Upvotes: 1
Views: 919
Reputation: 3083
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."'";
Upvotes: 1
Reputation: 780724
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>";
Upvotes: 3