c_pattle Posted May 10, 2010 Share Posted May 10, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/201263-key/ Share on other sites More sharing options...
kenrbnsn Posted May 10, 2010 Share Posted May 10, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/201263-key/#findComment-1055839 Share on other sites More sharing options...
ignace Posted May 10, 2010 Share Posted May 10, 2010 Instead of key2, key3 you can use: ?key[]=value&key[]=value&key[]=value or ?key=value1,value2,value3 Quote Link to comment https://forums.phpfreaks.com/topic/201263-key/#findComment-1055850 Share on other sites More sharing options...
c_pattle Posted May 10, 2010 Author Share Posted May 10, 2010 Awesome, thanks guys! Quote Link to comment https://forums.phpfreaks.com/topic/201263-key/#findComment-1055856 Share on other sites More sharing options...
c_pattle Posted May 11, 2010 Author Share Posted May 11, 2010 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" Quote Link to comment https://forums.phpfreaks.com/topic/201263-key/#findComment-1056290 Share on other sites More sharing options...
sharp.mac Posted May 11, 2010 Share Posted May 11, 2010 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> Quote Link to comment https://forums.phpfreaks.com/topic/201263-key/#findComment-1056314 Share on other sites More sharing options...
Adam Posted May 11, 2010 Share Posted May 11, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/201263-key/#findComment-1056322 Share on other sites More sharing options...
sharp.mac Posted May 11, 2010 Share Posted May 11, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/201263-key/#findComment-1056419 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.