Falanas Posted September 25, 2012 Share Posted September 25, 2012 Forgive me, I am only but a beginner in PHP & Mysql. I am trying to do a simple website but unfortunately I can see myself in a big mess: Getting 'id' from URL has been so hard for me and that's why I will appreciate if you could assist me. Here goes the code: index.php <?php //The first category (KENYA SAFARIS AS DEFAULT VALUE) of table - categoties $catsql = "SELECT * FROM categories WHERE categories.id = 1"; $catresult = mysql_query($catsql); while($catrow = mysql_fetch_assoc($catresult)){ echo "<h2> " .$catrow['cat_name'] . "</h2>"; //list of available sites to be visited (OF - TABLE tourism sites) $sitesql = "SELECT * FROM tourism_sites WHERE cat_id = " .$catrow['id'] . ";"; $siteresult = mysql_query($sitesql); $sitenumrows = mysql_num_rows($siteresult); if($sitenumrows==0){ echo "<h2 class = 'results'>No listed safari site</h2>"; } else{ while($sitenumrows = mysql_fetch_assoc($siteresult)){ echo "<ul>"; echo "<li><a href='#.php?id =" . $sitenumrows['id'] . "'>" . $sitenumrows['hotel_name'] . "</a></li>"; echo "</ul>"; } } echo "</li> "; } ?> <li> <?php //The second category (UGANDA AS DEFAULT VALUE) of categories table $cat2sql = "SELECT * FROM categories WHERE categories.id = 2"; $cat2result = mysql_query($cat2sql); while($cat2row = mysql_fetch_assoc($cat2result)){ echo "<h2>" .$cat2row['cat_name'] . "</h2>"; //list sites in Uganda to be visited $site2sql = "SELECT * FROM tourism_sites WHERE cat_id =" . $cat2row['id'] .";"; $site2result = mysql_query($site2sql); $site2numrows = mysql_num_rows($site2result); if($site2numrows==0){ echo "<h2 class = 'results'> No saved locations..! </h2>"; } else{ while($site2numrows = mysql_fetch_assoc($site2result)){ echo "<ul>"; echo "<li><a href='#.php?id =" . $site2numrows['id'] . "'>" . $site2numrows['hotel_name'] . "</a></li>"; echo "</ul>"; } } echo "</li>"; } ?> <?php if(isset($_GET['id']) == TRUE) { if(is_numeric($_GET['id']) == FALSE) { header("Location: " . BASE_DIR); } $correct_id = $_GET['id']; } else { header("Location: " . BASE_DIR); } ?> <!-- start page --> <div id="page"> <!-- start content --> <div id="content"> <div class="post"> <h2>Reservation</h2> <?php /* *this query links to variable . $sitenumrows['id'] of index.php to display further information about * $site2numrows['hotel_name'] */ $placesql = "SELECT tourism_sites.*, towns.*, place.*, photos.* FROM tourism_sites, towns, place, photos WHERE tourism_sites.cat_id = towns.id AND towns.id = place.cat_id AND place.id = photos.id AND photos.image_id = " . $correct_id; $placeresult = mysql_query($placesql); $placenumrow = mysql_num_rows($placeresult); if($placenumrow == 0){ echo "<h2 class = 'results'>There was a problem querrying database</h2>"; } else{ while ($placenumrow == mysql_fetch_assoc($placeresult)){ echo "<h2 class = 'title'>" . $placenumrow['town_name'] . "</p>"; } } ?> please help Quote Link to comment https://forums.phpfreaks.com/topic/268772-getting-variable-from-url-in-php-mysql/ Share on other sites More sharing options...
Jessica Posted September 25, 2012 Share Posted September 25, 2012 You never try to use $_GET['id'] as far as I can see. Quote Link to comment https://forums.phpfreaks.com/topic/268772-getting-variable-from-url-in-php-mysql/#findComment-1380789 Share on other sites More sharing options...
Christian F. Posted September 25, 2012 Share Posted September 25, 2012 (edited) Also, please use the [code][/code] tags around your code, as it helps make both your post and your code a lot easier to read. Thank you. Edited September 25, 2012 by Christian F. Quote Link to comment https://forums.phpfreaks.com/topic/268772-getting-variable-from-url-in-php-mysql/#findComment-1380807 Share on other sites More sharing options...
Falanas Posted September 26, 2012 Author Share Posted September 26, 2012 You never try to use $_GET['id'] as far as I can see. Jessica I believe I have used it (it is saved in '$correct_id'). What worries me is that this code is unable to execuite: [$placesql = "SELECT tourism_sites.*, towns.*, place.*, photos.* FROM tourism_sites, towns, place, photos WHERE tourism_sites.cat_id = towns.id AND towns.id = place.cat_id AND place.id = photos.id AND photos.image_id = " . $correct_id;] No any number e.g 1, 2, 3 etc is seen on the URL instead, it redirects me to BASE_DIR i.e header("Location: " . BASE_DIR); Is it because the id sent from index.php to the url does not meet the below conditions? <?php if(isset($_GET['id']) == TRUE) { if(is_numeric($_GET['id']) == FALSE) { header("Location: " . BASE_DIR); } $correct_id = $_GET['id']; } else { header("Location: " . BASE_DIR); } ?> I expected to see something like - details.php?id=1 or details.php?id=2 depending on the link clicked. What do you say? Thanks. =========================================================================================================== <?php if(isset($_GET['id']) == TRUE) { if(is_numeric($_GET['id']) == FALSE) { header("Location: " . BASE_DIR); } $correct_id = $_GET['id']; } else { header("Location: " . BASE_DIR); } ?> <!-- start page --> <div id="page"> <!-- start content --> <div id="content"> <div class="post"> <h2>Reservation</h2> <?php /* *this query links to variable . $sitenumrows['id'] of index.php to display further information about * $site2numrows['hotel_name'] */ $placesql = "SELECT tourism_sites.*, towns.*, place.*, photos.* FROM tourism_sites, towns, place, photos WHERE tourism_sites.cat_id = towns.id AND towns.id = place.cat_id AND place.id = photos.id AND photos.image_id = " . $correct_id; $placeresult = mysql_query($placesql); $placenumrow = mysql_num_rows($placeresult); if($placenumrow == 0){ echo "<h2 class = 'results'>There was a problem querrying database</h2>"; } else{ while ($placenumrow == mysql_fetch_assoc($placeresult)){ echo "<h2 class = 'title'>" . $placenumrow['town_name'] . "</p>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/268772-getting-variable-from-url-in-php-mysql/#findComment-1380981 Share on other sites More sharing options...
Jessica Posted September 26, 2012 Share Posted September 26, 2012 (edited) In case you missed it the last time: USE CODE TAGS! See the link in my signature on debugging SQL. What is the URL of the page? What do you see when you print_r($_GET)? Edited September 26, 2012 by Jessica Quote Link to comment https://forums.phpfreaks.com/topic/268772-getting-variable-from-url-in-php-mysql/#findComment-1381048 Share on other sites More sharing options...
White_Lily Posted September 27, 2012 Share Posted September 27, 2012 Okay so getting anything from the URL you should have right at the very top of your page (before <doctype>) you need: $Url = $_GET["id"]; /*or whatever you getting from url*/ Quote Link to comment https://forums.phpfreaks.com/topic/268772-getting-variable-from-url-in-php-mysql/#findComment-1381276 Share on other sites More sharing options...
Jessica Posted September 27, 2012 Share Posted September 27, 2012 Okay so getting anything from the URL you should have right at the very top of your page (before <doctype>) you need: $Url = $_GET["id"]; /*or whatever you getting from url*/ He does have a line for it. $correct_id = $_GET['id']; It doesn't have to come before the doctype, especially if there's other processing. You first need to actually check if it even exists, so I would put all the processing in one place. The only reason for it to be before the doctype is if you are going to redirect to another page based on that value or lack of value. Quote Link to comment https://forums.phpfreaks.com/topic/268772-getting-variable-from-url-in-php-mysql/#findComment-1381297 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.