Home>

I want to retrieve the date of birth registered in the database and use it as the initial pull-down value.
The database is registered in a single column called birth, and the pull-down is divided by year, month, and day.
How can I enter the initial value from here?

<! DOCTYPE html>
<html lang = "en">
    <head>
        <meta http-equiv = "Content-Type" content = "text/html;charset = UTF-8">
        <title>change</title>
    </head>
    <body>
        <h2>Change registered information</h2>
            <? php
                $num = htmlspecialchars ($_ GET ["id"]);
                $pdo = new PDO ('mysql: dbname = staff_manager;host = localhost;charset = utf8', 'user', 'passwar');
                $stmt = $pdo->prepare ("select name, class, birth from staff_master where ID = $num");
                $stmt->execute ();
                $result = $stmt->fetch (PDO :: FETCH_ASSOC);
            ?>
            <form action = "chan_ch.php" method = "get">
                <table>
                    <tr><td>Employee NO</td><td><? php echo $_GET ['id'];?></td></tr>
                    <tr><td>Name</td><td><input type = "text" name = "n_name" value = "<? php echo $result ['name']?>" /></td></tr>
                    <tr><td>Affiliation</td><td><input type = "text" name = "n_class" value = "<? php echo $result ['class']?>" /></td></tr>
                    <tr><td>Birthday</td><td>
                <? php
                        // year pulldown
                        echo "<select name = 'year'>";
                        for ($y = 1900;$y<date (Y) +1;$y ++) {
                            echo "<option value =". $y. "selected>". $y. "</option>\ n";
                        } echo "</select>year";
                        // month pulldown
                        $a = array ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", " 12 ");
                        echo "<select name = 'month'>";
                        foreach ($a as $key =>$value) {
                            $b = $key + 1;
                            echo "<option value =". $b. "selected>". $b. "</option>\ n";
                        } echo "</select>Month";
                        // Day pulldown
                        echo "<select name = 'day'>";
                        for ($m = 1;$m<= 31;$m ++) {
                            echo "<option value =". $m. "selected>". $m. "</option>\ n";
                        } echo "</select>Day";
                    ?>
                    </td></tr>
                </table>
                <input type = "submit" value = "confirm" />
                <input type = "button" onClick = "location.href = 'master.php'" value = "cancel" />
            </form>
    </body>
</html>
php
  • Answer # 1

    This is an example when you want to set an arbitrary value as an initial value, not just a database.

    $default_year = "2000";
    echo "<select name = 'year'>";
    for ($y = 1900;$y<date ('Y') + 1;$y ++) {
        $y_select = "";
        if ($default_year == $y) {
            $y_select = "selected";
        }
        echo '<option value = "'. $y. '"'. $y_select. '>'. $y. "</option>\ n";
    }
    echo "</select>year";


    I made some adjustments.

    date (Y) is used, but if there is no constant Y, an undefined error will occur.
    So we passed the format as a string like date ('Y').
    I don't have "quote" in the attribute of option because I don't like it personally.

    The value of DB can be obtained only for years, for example, as follows.

    $birth_year = date ("Y", strtotime ($result ["birth"]));


    Then just do the same for m and d.
    I have to worry about the number of digits.

    What I noticed:

    How about putting the value as it is even though you are using pdo?
    The value received with GET can be even worse.
    Shouldn't it bind?
    Prepared statements and stored procedures

    Stop inline style.

    PHP processing and echo are mixed in html, so readability is very bad.
    For PHP, write all the processing code before the declaration of<html> ;, and put everything in PHP into variables and just make echo? Because<select name ='year'>or html.

    $year_option = '';
    for ($y = 1900;$y<date ('Y') + 1;$y ++) {
        $y_select = "";
        if ($default_year == $y) {
            $y_select = "selected";
        }
        $year_option. = '<option value = "'. $y. '"'. $y_select. '>'. $y. "</option>\ n";
    }
    ?>
    <! DOCTYPE html>
    <html lang = "en">
        <head>
    <!-omitted->
    <select name = "year">
    <? = $year_option?>
    </select>year

    Isn't the id going to the confirmation screen if it is the same?

  • Answer # 2

    If you extract the date from the db, it will be in YYYY-MM-DD format. Is it like this?

    $birth = "2000-06-15";
    list ($by, $bm, $bd) = array_map (function ($x) {return (int) $x;}, explode ("-", $birth));
    echo "<select name = 'year'>\ n";
    foreach (range (1900, date ("Y") + 1) as $y) {
      $selected = $y == $by? "selected": "";
      echo "<option value = '{$y}' {$selected}>{$y}</option>\ n";
    }
    echo "</select>year";
    echo "<select name = 'month'>\ n";
    foreach (range (1,12) as $m) {
      $selected = $m == $bm? "selected": "";
      echo "<option value = '{$m}' {$selected}>{$m}</option>\ n";
    }
    echo "</select>Month";
    echo "<select name = 'day'>\ n";
    foreach (range (1,31) as $d) {
      $selected = $d == $bd? "selected": "";
      echo "<option value = '{$d}' {$selected}>{$d}</option>\ n";
    }
    echo "</select>day";