imimin Posted July 8, 2009 Share Posted July 8, 2009 I am definitely a rookie with php and hope I am asking this right! What I am trying to do is send the id of the product to the next page and in that page load the product description (desc). What I have so far is: <?php $cat = $_GET['cat']; $get_items = "SELECT * FROM poj_products WHERE cat='$cat'"; $get_items = mysql_query($get_items); echo '<a href="'.$sitelocation.$item_row['url'].'?item_desc='.$item_row['id'].'>view details/order</a>'; ?> but url link is not echoed. Any ideas on this? Thank you! Link to comment https://forums.phpfreaks.com/topic/165217-solved-help-needed-with-passing-id-through-url/ Share on other sites More sharing options...
gevans Posted July 8, 2009 Share Posted July 8, 2009 You're executing the query, but not assigning its results; <?php $cat = $_GET['cat']; $get_items = "SELECT * FROM poj_products WHERE cat='$cat'"; $get_items = mysql_query($get_items); $results = mysql_fetch_assoc($get_items); echo '<a href="'.$sitelocation.$results['url'].'?item_desc='.$iresults['id'].'>view details/order</a>'; Link to comment https://forums.phpfreaks.com/topic/165217-solved-help-needed-with-passing-id-through-url/#findComment-871230 Share on other sites More sharing options...
p2grace Posted July 8, 2009 Share Posted July 8, 2009 You should also protect the $cat var from sql injection. $cat = mysql_real_escape_string($_GET['cat']); Link to comment https://forums.phpfreaks.com/topic/165217-solved-help-needed-with-passing-id-through-url/#findComment-871232 Share on other sites More sharing options...
gevans Posted July 8, 2009 Share Posted July 8, 2009 You should also protect the $cat var from sql injection. $cat = mysql_real_escape_string($_GET['cat']); If it's an id I wouldn't use mysql_real_esacpe_string: $_GET['cat'] = (int) $_GET['cat']; Link to comment https://forums.phpfreaks.com/topic/165217-solved-help-needed-with-passing-id-through-url/#findComment-871239 Share on other sites More sharing options...
p2grace Posted July 8, 2009 Share Posted July 8, 2009 That works too Link to comment https://forums.phpfreaks.com/topic/165217-solved-help-needed-with-passing-id-through-url/#findComment-871241 Share on other sites More sharing options...
aggrav8d Posted July 8, 2009 Share Posted July 8, 2009 $cat = mysql_real_escape_string($_GET['cat']); $r = "SELECT * FROM poj_products WHERE cat='$cat'"; $get_items = mysql_query($r); $results = mysql_fetch_assoc($get_items); mysql_free_result($r); // please be tidy echo '<a href="'.$sitelocation.$results['url'].'?item_desc='.$iresults['id'].'>view details/order</a>'; Link to comment https://forums.phpfreaks.com/topic/165217-solved-help-needed-with-passing-id-through-url/#findComment-871242 Share on other sites More sharing options...
gevans Posted July 8, 2009 Share Posted July 8, 2009 I wouldn't say using mysql_free_result() was being tidy mysql_free_result() only needs to be called if you are concerned about how much memory is being used for queries that return large result sets. All associated result memory is automatically freed at the end of the script's execution. Really not necessary in this instance, and the result memory is free'd up when the script finish. Link to comment https://forums.phpfreaks.com/topic/165217-solved-help-needed-with-passing-id-through-url/#findComment-871245 Share on other sites More sharing options...
imimin Posted July 8, 2009 Author Share Posted July 8, 2009 You're executing the query, but not assigning its results; <?php $cat = $_GET['cat']; $get_items = "SELECT * FROM poj_products WHERE cat='$cat'"; $get_items = mysql_query($get_items); $results = mysql_fetch_assoc($get_items); echo '<a href="'.$sitelocation.$results['url'].'?item_desc='.$iresults['id'].'>view details/order</a>'; After modifying the code above to suite my situation to: <?php $cat = $_GET['cat']; $get_items = "SELECT * FROM poj_products WHERE cat='$cat'"; $get_items = mysql_query($get_items); $item_row = mysql_fetch_assoc($get_items); echo '<a href="'.$sitelocation.$item_row['url'].'?item_desc='.$item_row['id'].'>view details/order</a>'; ?> It is still not working? Still not echoing?? Link to comment https://forums.phpfreaks.com/topic/165217-solved-help-needed-with-passing-id-through-url/#findComment-871251 Share on other sites More sharing options...
p2grace Posted July 8, 2009 Share Posted July 8, 2009 Try the following, it'll display mysql errors and php errors. <?php /* Enable displaying of errors */ error_reporting(E_ALL); ini_set('display_errors', 'On'); $cat = (int) $_GET['cat']; $get_items = "SELECT * FROM poj_products WHERE cat='$cat'"; $get_items = mysql_query($get_items); if($get_items){ $item_row = mysql_fetch_assoc($get_items); echo '<a href="'.$sitelocation.$item_row['url'].'?item_desc='.$item_row['id'].'>view details/order</a>'; }else{ die("Unable to connect to database.".mysql_error()); } ?> Link to comment https://forums.phpfreaks.com/topic/165217-solved-help-needed-with-passing-id-through-url/#findComment-871256 Share on other sites More sharing options...
imimin Posted July 8, 2009 Author Share Posted July 8, 2009 Thank you! Now it is giving me the following error: Notice: Undefined index: cat in /homepages/27/d120150310/htdocs/poj/test1.php on line 42 Line 42 is: $cat = (int) $_GET['cat']; Link to comment https://forums.phpfreaks.com/topic/165217-solved-help-needed-with-passing-id-through-url/#findComment-871260 Share on other sites More sharing options...
wildteen88 Posted July 8, 2009 Share Posted July 8, 2009 You need to check that $_GET['cat'] exists first before using it if(isset($_GET['cat']) && is_numeric($_GET['cat'])) { $cat = (int) $_GET['cat']; $get_items = "SELECT * FROM poj_products WHERE cat='$cat'"; $get_items = mysql_query($get_items); if($get_items){ $item_row = mysql_fetch_assoc($get_items); echo '<a href="'.$sitelocation.$item_row['url'].'?item_desc='.$item_row['id'].'>view details/order</a>'; }else{ die("Unable to connect to database.".mysql_error()); } } Link to comment https://forums.phpfreaks.com/topic/165217-solved-help-needed-with-passing-id-through-url/#findComment-871262 Share on other sites More sharing options...
imimin Posted July 8, 2009 Author Share Posted July 8, 2009 It does exist. What does the error: Notice: Undefined index: cat in /homepages/27/d120150310/htdocs/poj/test1.php on line 42 mean? Its the: $cat = $_GET['cat']; line. I(t looks like it is defined to me?? Link to comment https://forums.phpfreaks.com/topic/165217-solved-help-needed-with-passing-id-through-url/#findComment-871288 Share on other sites More sharing options...
imimin Posted July 8, 2009 Author Share Posted July 8, 2009 I am sorry if my questions are to simplistic, I am q rookie at this stuff and I am trying to learn. I would appreciate any help I can get. Thank you! Link to comment https://forums.phpfreaks.com/topic/165217-solved-help-needed-with-passing-id-through-url/#findComment-871331 Share on other sites More sharing options...
p2grace Posted July 8, 2009 Share Posted July 8, 2009 Does echoing the value of the $_GET var work? echo $_GET['cat']; If it doesn't then the $_GET var isn't accessible from the url. Link to comment https://forums.phpfreaks.com/topic/165217-solved-help-needed-with-passing-id-through-url/#findComment-871335 Share on other sites More sharing options...
imimin Posted July 8, 2009 Author Share Posted July 8, 2009 No. I now have: <?php /* Enable displaying of errors */ error_reporting(E_ALL); ini_set('display_errors', 'On'); $cat = $_GET['cat']; $get_items = "SELECT * FROM poj_products WHERE cat='$cat'"; $get_items = mysql_query($get_items); if($get_items){ $item_row = mysql_fetch_assoc($get_items); echo '<a href="'.$sitelocation.$item_row['url'].'?item_desc='.$item_row['id'].'>view details/order</a>'; echo $_GET['cat']; }else{ die("Unable to connect to database.".mysql_error()); } ?> Does everything look right? Link to comment https://forums.phpfreaks.com/topic/165217-solved-help-needed-with-passing-id-through-url/#findComment-871343 Share on other sites More sharing options...
p2grace Posted July 8, 2009 Share Posted July 8, 2009 Can you show us the exact url in the address bar for this page? Link to comment https://forums.phpfreaks.com/topic/165217-solved-help-needed-with-passing-id-through-url/#findComment-871344 Share on other sites More sharing options...
imimin Posted July 8, 2009 Author Share Posted July 8, 2009 echo '<a href="'.http://www.patternsofjoy.com/test1.php.'?item_desc='.$item_row['id'].'>view details/order</a>'; Link to comment https://forums.phpfreaks.com/topic/165217-solved-help-needed-with-passing-id-through-url/#findComment-871358 Share on other sites More sharing options...
p2grace Posted July 8, 2009 Share Posted July 8, 2009 No I mean the exact url that contains the $_GET['cat'] script. So http://www.sample.com/filename.php?cat=3 Should be something like that... I want to confirm the $_GET vars. Link to comment https://forums.phpfreaks.com/topic/165217-solved-help-needed-with-passing-id-through-url/#findComment-871362 Share on other sites More sharing options...
imimin Posted July 8, 2009 Author Share Posted July 8, 2009 <A HREF="<?php echo "$sitelocation" . "gallery." . $phpex . "?" . "cat=" . "child"?> Link to comment https://forums.phpfreaks.com/topic/165217-solved-help-needed-with-passing-id-through-url/#findComment-871379 Share on other sites More sharing options...
p2grace Posted July 8, 2009 Share Posted July 8, 2009 If echoing the $_GET['cat'] isn't displaying anything, then the variable?cat=child isn't accessible via the url. Could you paste the actual url (the actual results of your previous post) so I can confirm that the variable is present and rule that out as an issue. Link to comment https://forums.phpfreaks.com/topic/165217-solved-help-needed-with-passing-id-through-url/#findComment-871383 Share on other sites More sharing options...
imimin Posted July 8, 2009 Author Share Posted July 8, 2009 Sorry! I had things set up a bit wrong. Things are set up properly now, BUT I am getting the error: Notice: Undefined index: cat in /homepages/27/d120150310/htdocs/poj/test1.php on line 64 Line 64 is: $cat = (int) $_GET['cat']; But it is defined, right? Link to comment https://forums.phpfreaks.com/topic/165217-solved-help-needed-with-passing-id-through-url/#findComment-871399 Share on other sites More sharing options...
p2grace Posted July 8, 2009 Share Posted July 8, 2009 do an isset() to check if the field exists if(!isset($_GET['cat'])){ die("Category isn't specified"); } Link to comment https://forums.phpfreaks.com/topic/165217-solved-help-needed-with-passing-id-through-url/#findComment-871403 Share on other sites More sharing options...
imimin Posted July 8, 2009 Author Share Posted July 8, 2009 returns: 'Category isn't specified' What exactly does that mean? Link to comment https://forums.phpfreaks.com/topic/165217-solved-help-needed-with-passing-id-through-url/#findComment-871405 Share on other sites More sharing options...
imimin Posted July 8, 2009 Author Share Posted July 8, 2009 I put this in before the script, at the beginning just after the <?php tag. Link to comment https://forums.phpfreaks.com/topic/165217-solved-help-needed-with-passing-id-through-url/#findComment-871407 Share on other sites More sharing options...
p2grace Posted July 8, 2009 Share Posted July 8, 2009 It means that there is no "?cat=child" in the url. So the script attempts to pull the $_GET['cat'] but can't because the variable doesn't exist in the url. You need to make sure the variable exists in the url before the script can pull the category and execute the query. Make sense? Link to comment https://forums.phpfreaks.com/topic/165217-solved-help-needed-with-passing-id-through-url/#findComment-871410 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.