Jump to content

Multiple JOINs


codeline

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/213845-multiple-joins/
Share on other sites

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 />
        ';
    
    }

}

?>

Link to comment
https://forums.phpfreaks.com/topic/213845-multiple-joins/#findComment-1113078
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/213845-multiple-joins/#findComment-1113857
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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