Customer:
Description:
Number:
Street:
City:
Province:
Postal Code:




Last nameFirst nameAddresses
AjerschAlexdesc: 0 stdsfsd: 3423 saddsfsd: 3423 saddes: 123 22des: 123 22Home: 123 March Roaddes: 123 22test: test testtest: test testtest: test testtest: 213123 123123test: 213123 123123yil: test: 5067 mongoliatest2: 5067 Queent: 45 0405test2: 5067 QueenNo: 2 MineNo: 2 Mine
AndersonMaseyEscape: 22 Jumpstreettest: 42 Test Sttest: 42 Test StThisisatest: 3424 Westcarletondsa: 12 fswed: d dd: d d
AshbyEthanCottage: 24601 Newbie_Sthomed: 124 phphpmongolia: 737-1111 f'(x)=7xASD: ASD SD
BannerBruceWork: 1 Cave_Parkhome: 239057 brick
BeeblebroxZaphodCottage: 12 mellow_stWork: 31 Street St.lkjhgkjghkj: 42 hitchhiker's wayfgbsfg: wgwr dgfhfdg: vcxbs dfsadf
CrosbieSammyHot: 1568 Old Brooke RoadHot: 1568 Old Brooke RoadNo: 2 Mine
DelosLucasHome: 206 Hitchhikers_WayHOME: 7-0 unitedHOME: 7-0 unitedHOME: 7-0 united
DentArthurThisDescript: 61345 Testing3432Home: 42 Hitchhiker Rd.STADIUM: 4-1 CAMP NOUhere: w where: w wCottage: 1 Giorgina Stdiznov: 89u43896 lkknesf
GyetkoDanteHome: 1337 Foobar_RdDumg: Mine Stat
HamiltonsHelencottage: 2589 t: t t
HawkTonyHome: 234 Amanda Sthome: 21 skate boardmongolia: e e
HickmansJohnCottage: 12 TestCottage: 12 Test
HolmesAzazHome: 11 Fifth_AveHello: 342 Hello 45stHello: 342 Hello 45st
MooreAmelia
MooreIsaacBIG HOUSE: 321 tree ave.
PicketDavisSchool: 3088 Dunrobin Rd.Home: 23 Oakville Drive
PinkyJackHome: 2 TURTLE_STREETCottagee: 42 turt_st home: 70 buttadjklsa: 2324 ASjdllkParis France: 72312 PSGTesting123: 4234 asdasd home: 16 Lismer Crescent
ScottMichaelCottage: 1 TEst-StHome: 1234 Dove
SmithElaHome: 113 Artist_RdCottage: 113 Artist_Rdskdhsajkhdjksa: sadlmsanm,d asd,mn
TrumpetJoshuaWork: 123 Worker_StWork: 123 Worker_StWOAH: 928 Kingsview CourtWOAH: 928 Kingsview CourtWork: 123 Worker_St
WillowbeePutnumwad: e435 awdga




END OF OUTPUT




Source Code of php-db-challenge3-solution.php


<?php
require("database.php");
if(isset(
$_POST['desc'])){
    
// print_r($_POST);
    // Array
    // (
    //     [cust_id] => 33
    //     [desc] => Test Addr
    //     [num] => 42
    //     [street] => Hitchhiker's way
    //     [city] => Southampton
    //     [prov] => ON
    //     [postal] => H0H0H0
    // )
    
$q $pdo->prepare("INSERT INTO addresses (addr_cust,addr_desc,addr_number,addr_street,addr_city,addr_prov,addr_postal)
                                            values(:cust,:desc,:num,:street,:city,:prov,:postal)"
);
    
$q->bindParam("cust",$_POST['cust_id']);
    
$q->bindParam("desc",$_POST['desc']);
    
$q->bindParam("num",$_POST['num']);
    
$q->bindParam("street",$_POST['street']);
    
$q->bindParam("city",$_POST['city']);
    
$q->bindParam("prov",$_POST['prov']);
    
$q->bindParam("postal",$_POST['postal']);

    
$q->execute();
    echo 
"Address Added";
}
?>
<!doctype HTML>
<html>
    <head>
    <style>
        table,tr,th,td {
            border:1px solid black;
            border-collapse:collapse;
        }
        th,td {
            padding: 5px;
        }
    </style>
    </head>
    <body>
<?php
    $query 
$pdo->prepare("SELECT * FROM customers ORDER BY cust_lname,cust_fname");
    
$query->execute();
    
$custs $query->fetchAll(PDO::FETCH_ASSOC);

    function 
getAddrs($custID) {
        global 
$pdo;
        
$query=$pdo->prepare("SELECT * FROM addresses WHERE addr_cust = :id");
        
$query->bindParam(":id",$custID);
        
$query->execute();
        
$addrs $query->fetchAll(PDO::FETCH_ASSOC);
        return 
$addrs;
      }
?>
        <form action="" method="POST">
            Customer: <select name='cust_id'>
                <?php 
                    
foreach($custs as $c) {
                        echo 
"<option value='".$c['cust_id']."'>".$c['cust_lname'].", ".$c['cust_fname']."</option>\n";
                    }
                
?>
                </select></br>
            Description: <input type="text" name='desc'></br>
            Number: <input type="text" name='num'></br>
            Street: <input type="text" name='street'></br>
            City: <input type="text" name='city'></br>
            Province: <select name='prov'>
                <?php 
                    
foreach(['AB'=>"Alberta",'BC'=>"British Columbia",'MB'=>"Manitoba",'NB'=>"New Brunswick",'NL'=>"Newfoundland",'NS'=>"Nova Scotia",'NT'=>"NW Territories",'NU'=>"Nunavut",'ON'=>"Ontario",'PE'=>"Prince Edward Island",'QC'=>"Quebec",'SK'=>"Saskatchewan",'YK'=>"Yukon"] as $code=>$prov) {
                        echo 
"<option value='$code'>$prov</option>\n";
                    }
                
?>
                </select></br>
            Postal Code: <input type="text" name='postal'></br>
            </select>
            <br>
            <input type="submit" value="ADD ADDRESS">
        </form>
        <br><br>
        <hr>
        <table>
      <tr><th>Last name</th><th>First name</th><th>Addresses</th></tr>
        <?php
foreach ($custs as $c) {
    echo 
"<tr><td>".$c['cust_lname']."</td><td>".$c['cust_fname']."</td>";
    foreach(
getAddrs($c['cust_id']) as $a) {
  
// print_r($a);
  // [addr_id] => 1
  // [addr_cust] => 1
  // [addr_desc] => Addr1
  // [addr_number] => 3088
  // [addr_street] => Dunrobin Rd
  // [addr_city] => Dunrobin
  // [addr_prov] => ON
  // [addr_postal] => K0A1T0
      
echo "<td><b>".$a['addr_desc']."</b>: ".$a['addr_number']." ".$a['addr_street']."</td>";
    }
    echo 
"</tr>\n";
  }
        
?>
    </table>
    <?php //WILL DISPLAY CODE OF THIS FILE ON THE WEBPAGE//
######################################################################
echo "<br/><br/><hr><hr>";
echo 
"<h2 style='text-align:center'>END OF OUTPUT</h2>\n";
echo 
"<hr><hr>";
echo 
"<h2><br>Source Code of ".basename((string)__FILE__) . "</h2><hr>";
show_source(__FILE__);
#######################################################################
?>

    </body>
</html>