Jump to content

Archived

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

imimin

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

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!

Share this post


Link to post
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>';

Share this post


Link to post
Share on other sites

You should also protect the $cat var from sql injection.

 

$cat = mysql_real_escape_string($_GET['cat']);

Share this post


Link to post
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'];

Share this post


Link to post
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>';

Share this post


Link to post
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.

Share this post


Link to post
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??

Share this post


Link to post
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());
}

?>

Share this post


Link to post
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'];

Share this post


Link to post
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());
    }
}

Share this post


Link to post
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??

Share this post


Link to post
Share on other sites

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!

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
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?

Share this post


Link to post
Share on other sites

Can you show us the exact url in the address bar for this page?

Share this post


Link to post
Share on other sites

echo '<a href="'.http://www.patternsofjoy.com/test1.php.'?item_desc='.$item_row['id'].'>view details/order</a>';

Share this post


Link to post
Share on other sites

<A HREF="<?php echo "$sitelocation" . "gallery." . $phpex . "?" . "cat=" . "child"?>

Share this post


Link to post
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.

Share this post


Link to post
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?

Share this post


Link to post
Share on other sites

do an isset() to check if the field exists

 

if(!isset($_GET['cat'])){
   die("Category isn't specified");
}

Share this post


Link to post
Share on other sites

returns:

 

'Category isn't specified'

 

What exactly does that mean?

Share this post


Link to post
Share on other sites

I put this in before the script, at the beginning just after the <?php tag.

Share this post


Link to post
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?

Share this post


Link to post
Share on other sites

×

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.