Jump to content

key=


c_pattle

Recommended Posts

Hey everyone

 

I am using this code "childrens_products.php?key=cooking" and then depending on the key I am then querying from a mysql database.  For example.

 

if(isset($_GET['key'])){

  $keyword = mysql_real_escape_string($_GET['key']);

$cat_query = "select * from product_list where category='" . $keyword . "' and department='home'";

 

However does it have to read "key=cooking" or can it read "key2=garden" or "key3="bathroom" etc?

 

Also you set more than one key such as "childrens_products.php?key=cooking, key2=garden"?

 

Thanks for any help

 

Chris

Link to comment
Share on other sites

You define what's passed on the URL. It could be "xyz" or "anything" as long as you program your script to look for the information. As for passing multiple values, you could use "key1=xxxx&key2=yyyy&key3=gkvkv", but I prefer to use arrays, so it would be "key[]=xxxx&key[]=sdfsdf&key[]=sdfsdf".

 

Ken

Link to comment
Share on other sites

I'm trying to use the array method but having some problems.  Below is where I have set the keys. 

 

	<li><a href="childrens_products.php?key[1]=cooking">Cooking gift sets</a></li>

<li><a href="childrens_products.php?key[2]=garden">Garden gift sets</a></li>

 

And this is where I am trying to use those keys

 

if(isset($_GET['key'])){
   $keyword = mysql_real_escape_string($_GET['key']);
$cat_query = "select * from product_list where category='" . $keyword . "' and department='children'";
$rs = mysql_query ( $cat_query, $conn ) or die ('error query1' . mysql_error());

 

However when I try to run the code I get this error "Warning: mysql_real_escape_string() expects parameter 1 to be string, array given in /var/www/ribbons/childrens_products.php on line 91"

Link to comment
Share on other sites

Ok, just because someone likes to use arrays, doesn't always mean it's the best for your project. Your error is because you are trying to parse an array into a string.

 

If your only needing one, two or even three variables in the URL at once I would suggest a re-write to get get id the array idea as the scope of your project may not require arrays.

try this....

 

<li><a href="childrens_products.php?key=cooking">Cooking gift sets</a></li>
<li><a href="childrens_products.php?key=garden">Garden gift sets</a></li>

 

 

Link to comment
Share on other sites

Ok, just because someone likes to use arrays, doesn't always mean it's the best for your project. Your error is because you are trying to parse an array into a string.

 

To be perfectly honest, reading the OP's first post I got the impression he wanted to pass a dynamic number of keywords from multiple inputs -- or something along those lines. In that case an array would have been the best solution...

 

You don't like arrays?

Link to comment
Share on other sites

You don't like arrays?

 

Arrays are my friends, however for someone just starting out and may not grasp the idea, why over complicate things with => everywhere.

let them get the work flow and practice of pushing and pulling the variables one at a time.... then move onto arrays.

 

Especially is the user is just getting the hang of _GET or _POST.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.