Jump to content

Struggling With Get on Receiving Page


justlukeyou

Recommended Posts

Hi,

 

I have products page which products page and I am echoing the ID of each product into a link with the description of the product as the anchor text.

 

<a href="product.php?ID=<?php echo $row['ID']; ?>" class='productlink' ><?php echo $row['description']; ?></a>

 

However I am struggling to Get the information from the receiving 'product.php' page.

 

The code I have on the product.php page is as follows:

 

<?php 
$ID = 0;
if (isset($_GET['ID'])) {
  $ID = $_GET['ID'];
} 
?>


<?php

echo $_GET['price']; // outputs: 1

?>

 

 

However nothing is being echoed out.  I just a blank screen. Am I missing like capital letters.  My column is title 'ID'.

 

Do I need to query the database in someway on the receiving page or do I just use a different GET code?

 

 

Link to comment
Share on other sites

Looking at your code you have just assigned the value from the GET to the variable $ID. You have not echoed $ID.

 

Also I am assuming you are actually getting the ID from the database. May want to echo $row['ID'] to make sure.

 

 

<?php 
$ID = 0;
if (isset($_GET['ID'])) {
  $ID = $_GET['ID'];
} 
?>


<?php
echo $ID;
echo $_GET['price']; // outputs: 1

?>

Link to comment
Share on other sites

Brilliant thanks mate,

 

However, when I use the following code the first 2 echo the ID number only and not the price.  I am trying echo the price.  The last echo doesn't echo anything.  I have tried  a few variations of it.

 

<?php
echo $ID;
echo $_GET['price']; 
?>

<?php
echo $price;
echo $_GET['ID']; 
?>

<?php echo $row['ID']; ?>

Link to comment
Share on other sites

I am trying to display information using the ID from the previous page which is echoed into the link on that page.

 

So when someone clicks on ID product.php?ID=222 on say the homepage I can display all the information about ID 222 on the product.php page.

 

Im confused why 'price'  would be in link.  I thought only the ID would go in the link.

 

 

Link to comment
Share on other sites

Hi,

 

Could someone help me with this please.

 

I have inserted the ID number into the link but on the product.php page I cant Get the information for the product based on the ID number.

 

All I can is echo the ID.  But I am looking to echo information such as price and which is in the database.

 

Should I be echoing in the ID into here (product.php?ID=222) or the information I want to dispaly.

 

This is an example of what I am looking to do and someone can click on a product and display all the product information.  Do I use Get command based on the ID to do this?

 

http://www.play.com/Books/Books/-/4203/3470/3-/Refine.html

Link to comment
Share on other sites

the price needs to be retrieve from the database in order to get the price

 

$productid = $_GET['product_id'];
$sql = "SELECT * FROM table WHERE productid = '$productid'";
$query = mysql_query($sql);
$row = mysql_fetch_assoc($query)

echo $row['productid'].' '.$row['price'];

 

Will have to change the vales to match your field names but should be enough to get you started.

 

Ray

Link to comment
Share on other sites

http://www.example.com/index.php?price=123&id=321

 

Take that URL, swap out 'example' with your domain, and 'index.php' with whatever the path is to your file.

 

Put:

 

echo 'Price: '. $_GET['price'] .'<br/>ID: '. $_GET['id'];

 

in that file, and have a look at the results.

 

Hi,  I tried this and it echoed the price only, not the ID

 

So, it echo'd 123 as the price, but nothing as the id?  That can't be.

Link to comment
Share on other sites

Apologies, my ID is in capitals.  I change it to capitals and it worked fine.

 

Many thanks. 

 

I just thought in the link to the product page I just had to put the ID into, I didnt know I had to put other items such as price.

 

Many thanks.

 

Is it possible to make it work by just using the ID?

Link to comment
Share on other sites

you should just be sending the ID, that is all that is needed. Then you use the example above to retrieve the rest of the information out of the database. No need to put all kinds of data in the url.

 

Ray

Link to comment
Share on other sites

you should just be sending the ID, that is all that is needed. Then you use the example above to retrieve the rest of the information out of the database. No need to put all kinds of data in the url.

 

Ray

 

I just wanted to see if OP had a basic understanding of how $_GET worked.

 

And yes, all you have to do is simply pass the ID of the product in question, and query your products table with that ID, like the script posted above.

Link to comment
Share on other sites

So If use this link:

 

a href="product.php?ID=222

 

And use this code on the following page it will display the price?

 

<?php 
$ID = $_GET['ID'];
$sql = "SELECT * FROM table WHERE productdbase = '$ID'";
$query = mysql_query($sql);
$row = mysql_fetch_assoc($query)
?>

<?php 
echo $row['ID'].' '.$row['price'];
?>

Link to comment
Share on other sites

$sql = "SELECT * FROM table WHERE productdbase = '$ID'";

 

You will obviously have to ensure 'table' is your table name and 'productdbase' is the field name that stores your ID's.  And, of course, there will have to be a matching record in the table to that of $ID for it to return any results.

 

Use mysql_real_escape_string with your variables before inserting them into your queries, ie:

 

$sql = "SELECT * FROM table WHERE productdbase = '". mysql_real_escape_string($ID) ."'";

Link to comment
Share on other sites

 

Hi,  I have set up another page to try and do this.  I have articles.php which goes to article.php

 

<a href="article.php?ID=<?php echo $row['ID']; ?>" class='articleslink' rel="nofollow" ><?php echo $row['title']; ?></a>

 

This creates "article.php?ID=2

 

However I cant echo anything on article.php though.  This is the code I am using:

 

<?php

$sql = "SELECT * FROM table WHERE articles = '$ID'";
    ?>


<?php echo $_GET['title'];?>

<?php 
echo $row['ID'].' '.$row['title'];
?>

 

I have tried to use the escape string on both articles.php and article.php but this has no affect.

 

Any advice would be gratefully appreciated.

 

Link to comment
Share on other sites

Hi,

 

I have the following code but it says: Invalid query: Query was empty

 

 

<?php 



$sql = "SELECT * FROM articles WHERE '". mysql_real_escape_string($ID) ."'";
$rs = mysql_query($strSQL);

if (!$strSQL) { // add this check.
    die('Invalid query: ' . mysql_error());


}


// Loop the recordset $rs
// Each row will be made into an array ($row) using mysql_fetch_array
while($row = mysql_fetch_array($rs)) {


  echo $row['title'] . "<br />";

}

mysql_close();
?>

Link to comment
Share on other sites

$sql = "SELECT * FROM articles WHERE '". mysql_real_escape_string($ID) ."'";	
$rs = mysql_query($strSQL);	
if (!$strSQL) { // add this check.    die('Invalid query: ' . mysql_error());

 

You're naming your sql statement variable $sql but calling the query with $strSQL, that's why it's empty. Change one or the other.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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