0

我需要从三个下拉列表中的数据库中检索日期。Mysql & PHP 代码:

if (mysqli_connect_errno()) {
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else {
  $sql = "SELECT ui.`sex` AS ui_sex , 
  ui.`last_name` AS ui_lname, 
  ui.`first_name` AS ui_fname, 
  ui.`nickname` AS ui_nickname, 
  ui.`nin` AS ui_nin, 
  ui.`birth_date` AS ui_birth,             
  WHERE ui.`userid` = 1";
$result = mysqli_query($con, $sql);
$count = mysqli_num_rows($result);
if ($count == 0) {
  echo '<h4>No data submited yet.</h4>';
}
else{
   while ($row = mysqli_fetch_array($result)) {
      $ui_sex = $row['ui_sex'];
      $ui_nume = $row['ui_lname'];
      $ui_prenume = $row['ui_fname'];
      $ui_nickname = $row['ui_nickname'];
      $ui_nin = $row['ui_nin'];
      $ui_birth = $row['ui_birth'];
   }
}

HTML 代码:

<form action="submit_values.php" method="POST">
<!-- Code for previous fields -->
<label class='small-3 columns label_inf_clients'>Day of birth*:</label>
        <div class="small-6 columns select_tag_bottom_margin"> 
            <select id="select_day" name="day" >
    <?php
        //the next option should store the day from $birth_date
         echo'<option class="option_an" value='.$day.'>'.$day.'</option>';

    ?>
    <?php
    for ($i = 1; $i <= 31; $i++) {
        echo'<option  class="option_an" value=' . $i . ">" . $i . '<option>';
    }
    ?>
            </select>

       <select class="select_month" name="month">
            <?php
               //the next option should store the month from $birth_date
              echo'<option class="option_an" value='.$month.'>'.$month.'</option>';
            ?>
            <option value="01">Jan</option>
             .............................
            <option value="11">Nov</option>
            <option value="12">Dec</option>
        </select>
        <select class="select_year" name="an">
    <?php
        //the next option should store the year from $birth_date
              echo'<option class="option_an" value='.$year.'>'.$year.'</option>';
    for ($i = 2013; $i >= 1940; $i--) {
        echo'<option  class="option_an" value=' . $i . ">" . $i . '<option>';
    }
    ?>
    </select>
</form>

让我们回顾一下:我从数据库中选择存储在 $ui_birth 中的出生日期,我想将此日期拆分为 3 个下拉列表,如下图所示:https ://docs.google.com/file/d/0B44T4qmqQGHAY3JkdFZNOGdLTW8 /edit?usp=共享

4

2 回答 2

0

您缺少一些双引号和单引号,并且您没有正确关闭选项标签。

这个...

echo'<option class="option_an" value='.$day.'>'.$day.'</option>';

应该是这个……

echo'<option class="option_an" value="'.$day.'">'.$day.'</option>';

这个...

for ($i = 1; $i <= 31; $i++) {
    echo'<option  class="option_an" value=' . $i . ">" . $i . '<option>';
}

应该是这个……

for ($i = 1; $i <= 31; $i++) {
    echo'<option  class="option_an" value="' . $i . '">' . $i . '</option>';
}

这个...

echo'<option class="option_an" value='.$month.'>'.$month.'</option>';

应该是这个……

echo'<option class="option_an" value="'.$month.'">'.$month.'</option>';

这个...

for($i = 2013; $i >= 1940; $i--) {
    echo'<option  class="option_an" value=' . $i . ">" . $i . '<option>';
}

应该是这个……

for ($i = 2013; $i >= 1940; $i--) {
    echo'<option  class="option_an" value="' . $i . '">' . $i . '</option>';
}

此外,您应该将 MYSQLI_ASSOC 添加到您的 mysqli_fetch_array ...

while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))

你的sql语句也应该是这样的......

$sql = "SELECT sex AS ui_sex , last_name AS ui_lname, first_name AS ui_fname, nickname AS ui_nickname, nin AS ui_nin, birth_date AS ui_birth FROM ui WHERE ui.`userid` = 1"; 
于 2013-05-14T22:02:37.233 回答
0

您可以使用 php 的日期函数提取日期的组成部分,如下所示:

$ui_birth = $row['ui_birth'];
$birth_date = strtotime($ui_birth);
$day = date('d', $birth_date);
$month = date('m', $birth_date);
$year = date('Y', $birth_date);
于 2013-05-14T21:06:22.790 回答