codeline Posted September 19, 2010 Share Posted September 19, 2010 I've got a region-type select form where a user chooses his/her continent, and depending on the selection, a second select form is filled with country values relating to that continent. From there, depending on the country selection, a third select form is dynamically filled with regions relating to that country and continent. See the code attached: <form action="" method="POST"> <select id="continent" name="continent" style="min-width: 170px;"> <option value="">Select Continent</option> <?php $q = "SELECT * FROM table_areaContinent"; $r = mysql_query($q) or die(mysql_error()); while($row = mysql_fetch_array($r)){ $continent_id = $row['cont_id']; $continent = $row['continent']; print '<option value="' . $continent_id . '">' . $continent . '</option>' . "\n"; } ?> </select> <br /><br /> <select id="country" name="country" style="min-width: 170px;"> <option value="">Select Country</option> <?php $q = "SELECT * FROM table_areaCountry JOIN table_areaContinent USING (cont_id)"; $r = mysql_query($q) or die(mysql_error()); while($row = mysql_fetch_array($r)){ $country_id = $row['country_id']; $country = $row['country']; $continent_id = $row['cont_id']; print '<option value="' . $country_id . '" class="' . $continent_id . '">' . $country . '</option>' . "\n"; } ?> </select> <br /><br /> <select id="region" name="region" style="min-width: 170px;"> <option value="">Select Region</option> <?php $q = "SELECT * FROM table_areaRegion JOIN table_areaCountry USING (country_id)"; $r = mysql_query($q) or die(mysql_error()); while($row = mysql_fetch_array($r)){ $region_id = $row['region_id']; $region = $row['region']; $country_id = $row['country_id']; print '<option value="' . $region_id . '" class="' . $country_id . '">' . $region . '</option>' . "\n"; } ?> </select> <br /><br /> <input type="submit" value="Submit" name="submit" class="submit" /> </form> Now, I simply just want to echo out the Continent, Country and Region that the user selected after the form is submitted. I currently have this : <?php if(!empty($_POST['submit'])){ $continent = $_POST['continent']; $country = $_POST['country']; $region = $_POST['region']; print ' <b>Continent: ' . $continent . '<br /> <b>Country: ' . $country . '<br /> <b>Region: ' . $region . '<br /> '; } ?> Which echoes out the ID numbers for each variable. What would be the best direction to take when pulling data from multiple tables to echo out the actual Names that the IDs are assigned to? Quote Link to comment https://forums.phpfreaks.com/topic/213845-multiple-joins/ Share on other sites More sharing options...
codeline Posted September 20, 2010 Author Share Posted September 20, 2010 I played around and this seems to work. However, I wanted to see if there was anything wrong with going about it this way. <?php if(!empty($_POST['submit'])){ $continent = $_POST['continent']; $country = $_POST['country']; $region = $_POST['region']; $q = "SELECT * FROM areaContinent, areaCountry, areaRegion WHERE areaRegion.region_id = '$region' AND areaCountry.country_id = '$country' AND areaContinent.cont_id = '$continent'"; $r = mysql_query($q) or die(mysql_error()); while($row = mysql_fetch_array($r)){ $row_continent = $row['continent']; $row_country = $row['country']; $row_region = $row['region']; print ' <b>Continent: ' . $row_continent . '<br /> <b>Country: ' . $row_country . '<br /> <b>Region: ' . $row_region . '<br /> '; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/213845-multiple-joins/#findComment-1113078 Share on other sites More sharing options...
codeline Posted September 21, 2010 Author Share Posted September 21, 2010 Anyone got any advice on the above? Quote Link to comment https://forums.phpfreaks.com/topic/213845-multiple-joins/#findComment-1113854 Share on other sites More sharing options...
rwwd Posted September 21, 2010 Share Posted September 21, 2010 Oh Yes! $continent = mysql_real_escape_string(strip_tags($_POST['continent'])); $country = mysql_real_escape_string(strip_tags($_POST['country'])); $region = mysql_real_escape_string(strip_tags($_POST['region'])); Now your $_POST information is sql safe, leaving it unsanitised is a BAD idea, seriously. Rw Quote Link to comment https://forums.phpfreaks.com/topic/213845-multiple-joins/#findComment-1113857 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.