如何从数据库中回显出单选按钮中存储的值?

时间:2016-06-13 02:41:41

标签: php mysqli

我正在尝试回显特定数据的性别的当前值,并在用户想要时编辑性别。我怎么能用这段代码呢?

这是我的代码:

    echo "Gender: <label class=\"radio-inline\"><input type=\"radio\" name=\"gender\" value=\"Male\" > Male</label>
                  <label class=\"radio-inline\"><input type=\"radio\" name=\"gender\" value=\"Female\"> Female</label><br/>";

以下是我如何获取数据库中所有信息的完整代码:

       <?php 
        while(mysqli_stmt_fetch($stmt1)) {
        echo "First Name: <input type=\"text\" style=\"border: none; border-color: transparent;\" value=\"$first_name\"><br/>";
        echo "Middle Name: <input type=\"text\" style=\"border: none; border-color: transparent;\" value=\"$middle_name\"><br/>";
        echo "Last Name: <input type=\"text\" style=\"border: none; border-color: transparent;\" value=\"$last_name\"><br/>";
        echo "Age: <input type=\"text\" style=\"border: none; border-color: transparent;\" value=\"$age\"><br/>";
        echo "Birth Date: <select name=\"month\"  style=\"width:auto;\">
                          <option value=\"$month\"></option>
                          <option value=\"January\">January</option>
                          <option value=\"February\">February</option>
                          <option value=\"March\">March</option>
                          <option value=\"April\">April</option>
                          <option value=\"May\">May</option>
                          <option value=\"June\">June</option>
                          <option value=\"July\">July</option>
                          <option value=\"August\">August</option>
                          <option value=\"September\">September</option>
                          <option value=\"October\">October</option>
                          <option value=\"November\">November</option>
                          <option value=\"December\">January</option>
                          </select>
                          <select name=\"day\" style=\"width:auto;\">
                          <option value=\"\"></option>
                          <option value=\"1\">1</option>
                          <option value=\"2\">2</option>
                          <option value=\"3\">3</option>
                          <option value=\"4\">4</option>
                          <option value=\"5\">5</option>
                          <option value=\"6\">6</option>
                          <option value=\"7\">7</option>
                          <option value=\"8\">8</option>
                          <option value=\"9\">9</option>
                          <option value=\"10\">10</option>
                          <option value=\"11\">11</option>
                          <option value=\"12\">12</option>
                          <option value=\"13\">13</option>
                          <option value=\"14\">14</option>
                          <option value=\"15\">15</option>
                          <option value=\"16\">16</option>
                          <option value=\"17\">18</option>
                          <option value=\"19\">19</option>
                          <option value=\"20\">20</option>
                          <option value=\"21\">21</option>
                          <option value=\"22\">22</option>
                          <option value=\"23\">23</option>
                          <option value=\"24\">24</option>
                          <option value=\"25\">25</option>
                          <option value=\"26\">26</option>
                          <option value=\"27\">27</option>
                          <option value=\"28\">29</option>
                          <option value=\"30\">30</option>
                          <option value=\"31\">31</option>
                          </select>
                          <input type=\"text\" value=\"$year\" size=\"3\" style=\"border: none; border-color: transparent;\"><br/>";
        echo "Birth Place: <input type=\"text\" style=\"border: none; border-color: transparent;\" value=\"$birth_place\"><br/>";
        echo "Gender: <label class=\"radio-inline\">
              <input type=\"radio\" name=\"gender\" value=\"Male\" ";
              if($gender == 'Male') { echo 'Checked';}
              echo " > Male</label>
              <label class=\"radio-inline\">
              <input type=\"radio\" name=\"gender\" value=\"Female\" ";
              if($gender == 'Female') { echo 'Checked';}
              echo "> Female</label><br/>";
        echo "Marital Status: <input type=\"text\" style=\"border: none; border-color: transparent;\" value=\"$martial_status\"><br/>";
        echo "Religion: <input type=\"text\" style=\"border: none; border-color: transparent;\" value=\"$religion\"><br/>";
        echo "Nationality: <input type=\"text\" style=\"border: none; border-color: transparent;\" value=\"$nationality\"><br/>";
        echo "Email Address: <input type=\"text\" style=\"border: none; border-color: transparent;\" value=\"$email_address\" size=\"35\"><br/>";
        echo "Address 1: <input type=\"text\" style=\"border: none; border-color: transparent;\" value=\"$address_1\"><br/>";
        echo "Address 2: <input type=\"text\" style=\"border: none; border-color: transparent;\" value=\"$address_2\"><br/>";
        echo "Course: <input type=\"text\" style=\"border: none; border-color: transparent;\" value=\"$course\"><br/>";
        echo "School Graduated: <input type=\"text\" style=\"border: none; border-color: transparent;\" value=\"$school_graduated\" size=\"65\"><br/>";
        echo "Remarks: <input type=\"text\" style=\"border: none; border-color: transparent;\" value=\"$remarks\"><br/>";
        echo "<br/><br/>";
        echo "<center><a href=\"view_all_crew.php\"><button type=\"button\" class=\"btn btn-default\" >Cancel</button></a>&nbsp;";
        echo "<button type=\"button\" class=\"btn btn-success\" onclick=\"document.location = 'edit_crew.php?id=$id'; \">Continue</button></center>";
      }
      ?>

3 个答案:

答案 0 :(得分:1)

使用单选按钮中的if条件选择存储的值,如

$gender = 'Female';
echo "Gender: <label class=\"radio-inline\">
               <input type=\"radio\" name=\"gender\" value=\"Male\" ";
               if(isset($gender) && $gender == 'Male') { echo 'checked';}
 echo " > Male</label>
              <label class=\"radio-inline\">
              <input type=\"radio\" name=\"gender\" value=\"Female\" ";
              if(isset($gender) && $gender == 'Female') { echo 'checked';}
  echo "> Female</label><br/>";

答案 1 :(得分:1)

假设您在数据库表中存储了1个男性和0个女性,那么从数据库中获取的代码可以实现为:

<input type="radio" name="gender" <?php echo $row['gender'] === '1' ?'checked="checked"':'';?>/> Male
<input type="radio" name="gender" <?php echo $row['gender'] === '0' ?'checked="checked"':'';?>/> Female

多数民众赞成。如果男性存储在数据库中,那么将检查第一个,否则将检查第二个。

答案 2 :(得分:0)

尝试这样的事情,使用单引号作为echo,并使用双引号作为值,这很容易做到。

    <?php

    $gender = 'Male';

    echo 'Gender: <label class="radio-inline">
                   <input type="radio" name="gender" value="Male"';
                   if(isset($gender) && $gender == 'Male') { echo 'checked="checked"'; }
     echo ' > Male</label>
                  <label class="radio-inline">
                  <input type="radio" name="gender" value="Female" ';
                  if(isset($gender) && $gender == 'Female') { echo 'checked="checked"'; }
      echo '> Female</label><br/>';

    ?>