justlukeyou Posted May 17, 2012 Share Posted May 17, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/262693-struggling-with-get-on-receiving-page/ Share on other sites More sharing options...
craygo Posted May 17, 2012 Share Posted May 17, 2012 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 ?> Quote Link to comment https://forums.phpfreaks.com/topic/262693-struggling-with-get-on-receiving-page/#findComment-1346426 Share on other sites More sharing options...
justlukeyou Posted May 17, 2012 Author Share Posted May 17, 2012 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']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/262693-struggling-with-get-on-receiving-page/#findComment-1346433 Share on other sites More sharing options...
requinix Posted May 17, 2012 Share Posted May 17, 2012 product.php?ID= There is no "price" in the URL. Quote Link to comment https://forums.phpfreaks.com/topic/262693-struggling-with-get-on-receiving-page/#findComment-1346435 Share on other sites More sharing options...
justlukeyou Posted May 17, 2012 Author Share Posted May 17, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/262693-struggling-with-get-on-receiving-page/#findComment-1346437 Share on other sites More sharing options...
justlukeyou Posted May 18, 2012 Author Share Posted May 18, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/262693-struggling-with-get-on-receiving-page/#findComment-1346574 Share on other sites More sharing options...
requinix Posted May 18, 2012 Share Posted May 18, 2012 Have you written any code to actually get the information from the database? What is it? Quote Link to comment https://forums.phpfreaks.com/topic/262693-struggling-with-get-on-receiving-page/#findComment-1346635 Share on other sites More sharing options...
justlukeyou Posted May 18, 2012 Author Share Posted May 18, 2012 I have tried these codes but they just echo the ID. Not the price which I am trying to echo. <?php echo $ID; echo $_GET['price']; ?> <?php echo $price; echo $_GET['ID']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/262693-struggling-with-get-on-receiving-page/#findComment-1346640 Share on other sites More sharing options...
mrMarcus Posted May 18, 2012 Share Posted May 18, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/262693-struggling-with-get-on-receiving-page/#findComment-1346642 Share on other sites More sharing options...
craygo Posted May 18, 2012 Share Posted May 18, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/262693-struggling-with-get-on-receiving-page/#findComment-1346645 Share on other sites More sharing options...
justlukeyou Posted May 18, 2012 Author Share Posted May 18, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/262693-struggling-with-get-on-receiving-page/#findComment-1346648 Share on other sites More sharing options...
mrMarcus Posted May 18, 2012 Share Posted May 18, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/262693-struggling-with-get-on-receiving-page/#findComment-1346650 Share on other sites More sharing options...
justlukeyou Posted May 18, 2012 Author Share Posted May 18, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/262693-struggling-with-get-on-receiving-page/#findComment-1346653 Share on other sites More sharing options...
craygo Posted May 18, 2012 Share Posted May 18, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/262693-struggling-with-get-on-receiving-page/#findComment-1346655 Share on other sites More sharing options...
mrMarcus Posted May 18, 2012 Share Posted May 18, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/262693-struggling-with-get-on-receiving-page/#findComment-1346662 Share on other sites More sharing options...
justlukeyou Posted May 18, 2012 Author Share Posted May 18, 2012 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']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/262693-struggling-with-get-on-receiving-page/#findComment-1346675 Share on other sites More sharing options...
mrMarcus Posted May 18, 2012 Share Posted May 18, 2012 $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) ."'"; Quote Link to comment https://forums.phpfreaks.com/topic/262693-struggling-with-get-on-receiving-page/#findComment-1346676 Share on other sites More sharing options...
justlukeyou Posted May 18, 2012 Author Share Posted May 18, 2012 Hi, I tried that it didn't work if I just include the ID. It works if I include everything into the link. Its getting their though lol Quote Link to comment https://forums.phpfreaks.com/topic/262693-struggling-with-get-on-receiving-page/#findComment-1346678 Share on other sites More sharing options...
justlukeyou Posted May 19, 2012 Author Share Posted May 19, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/262693-struggling-with-get-on-receiving-page/#findComment-1346827 Share on other sites More sharing options...
justlukeyou Posted May 19, 2012 Author Share Posted May 19, 2012 Could somoene help me with this please. If I just have the ID how to I query the database using GET? Quote Link to comment https://forums.phpfreaks.com/topic/262693-struggling-with-get-on-receiving-page/#findComment-1346905 Share on other sites More sharing options...
justlukeyou Posted May 20, 2012 Author Share Posted May 20, 2012 Hi, Does anyone have a guide on how to read a table using Get please? I have spent another 4 hours on this today and I have progressed with this issue. In must be possible to read a database within just an ID? Quote Link to comment https://forums.phpfreaks.com/topic/262693-struggling-with-get-on-receiving-page/#findComment-1347068 Share on other sites More sharing options...
justlukeyou Posted May 20, 2012 Author Share Posted May 20, 2012 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(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/262693-struggling-with-get-on-receiving-page/#findComment-1347086 Share on other sites More sharing options...
craygo Posted May 21, 2012 Share Posted May 21, 2012 give me a the names of the fields in your table. and I will help you out. Quote Link to comment https://forums.phpfreaks.com/topic/262693-struggling-with-get-on-receiving-page/#findComment-1347371 Share on other sites More sharing options...
justlukeyou Posted May 21, 2012 Author Share Posted May 21, 2012 aha your a Legend. My links are currently stuffed with fields. I would love to sort it by ID. ID title intro articleintro image date Quote Link to comment https://forums.phpfreaks.com/topic/262693-struggling-with-get-on-receiving-page/#findComment-1347430 Share on other sites More sharing options...
TOA Posted May 22, 2012 Share Posted May 22, 2012 $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. Quote Link to comment https://forums.phpfreaks.com/topic/262693-struggling-with-get-on-receiving-page/#findComment-1347457 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.