Jump to content

[SOLVED] Help needed with passing 'id' through url


imimin

Recommended Posts

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
Share on other sites

  • Replies 92
  • Created
  • Last Reply

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
Share on other sites

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
Share on other sites

$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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

Archived

This topic is now archived and is closed to further replies.


×
×
  • 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.