Select con city

  <?php
        $server = "localhost";
        $user = "root";
        $password = "";
        $db = "sakila";
        try {
            $conn = new PDO("mysql:host=$server;dbname=$db", $user, $password);
            $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

            print_r($_GET);
            $address = filter_input(INPUT_GET, "address");
            $district = filter_input(INPUT_GET, "district");
            $city_id = filter_input(INPUT_GET, "city_id");
            $postal_code = filter_input(INPUT_GET, "postal_code");
            $phone = filter_input(INPUT_GET, "phone");
            if(!empty($address) && !empty($district) &&!empty($city_id) && !empty($phone)){
                $sql="insert into address (address,district,city_id,postal_code,phone) values ";
                $sql.="('$address','$district',$city_id,'$postal_code','$phone')";
                $conn->exec($sql);
            }
            $sql = "select city_id,city from city order by city";
            $resul = $conn->query($sql);
            $ciudades = $resul->fetchAll(PDO::FETCH_ASSOC);
        } catch (Exception $ex) {
            echo $ex->getMessage();
        }
        ?>
        <form>
            Dirección: <input type="text" name="address"><br/>
            Distrito: <input type="text" name="district"><br/>
            Ciudad: <select name="city_id">
                <option value="0">Seleccione la ciudad</option>
                <?php
                foreach ($ciudades as $ciudad) {
                    ?>
                    <option value="<?= $ciudad['city_id'] ?>"><?= $ciudad['city'] ?></option>
                    <?php
                }
                ?>

            </select><br/>
            Código postal: <input type="text" name="postal_code"><br/>
            Teléfono: <input type="text" name="phone"><br/>
            <input type="submit">
        </form>

Publicado por

Juan Pablo Fuentes

Formador de programación y bases de datos