Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Torger Angeltveit
Torger Angeltveit
11,228 Points

Passing a GET variable through a link with PHP

Hi, I have trouble recieving and re using the variable i send through a GET varible.

THE VARIABLE: is a result from the mysqli dabase:

$product_id = $row['produkt_id'];

THE LINK on the index.php:

<a href="pages.php?id=<?php echo $product_id; ?>">

the code on page.php

if($_GET['id']==$product_id){
   echo $product_id;
   } else {
   echo "failed";
   }

When i push the link the URL is correct: mywebsite.com/pages.php?id=27 But the value of the varible cant be used when i try to echo it out. The if statement returns "failed"

3 Answers

Hi Torger,

What you should consider doing is looking for the existence of the $_GET variable in the URL and then if it exists, save it to a variable.

<?php
if(isset($_GET['id'])) {
$product_id = $_GET['id'];
}
?>
Maximillian Fox
PLUS
Maximillian Fox
Courses Plus Student 9,236 Points

The best way you can check things is to echo out the variables that you are assigning and see if they are returning what you are expecting. This is a good example of how you can debug problems in your code :)

The best thing to do is start from when your variable first gets introduced. So as a starting point, you can try echoing out the product_id variable just after you declare it, like this.

<?php
  $product_id = $row['produkt_id'];
  echo $product_id;
?>

Now you can check if this gets printed to the screen, if so, echo out the next variable until you see where the problem is :)

Also I noticed 'produkt_id' in your $row array. Is this the correct spelling of the row, or should this be 'product_id' as well? If your $product_id is not echoing out, it could be a problem with your

$result = $mysqli->query($query); 

or

while($row = $result->fetch_assoc());

Check everywhere and make sure your query is returning results for you :)

Also, are you actually declaring $product_id on the page.php script somewhere? If not, it will always return false.

Torger Angeltveit
Torger Angeltveit
11,228 Points

Hi Maximillian Fox, and thanks for you're answer! However, i sort of found out the problem but i dont know how to solve it.

The $product_id variable doesnt get saved in any variable on pages.php. And it looks like the if method doesnt check if the URL $product_id variable value is equal to the $product_id value passed from the link.

if($_GET['id']==$product_id) {

I think this it where something is wrong. But i have no idea why?