首页 > 解决方案 > MySQL如何从复选框列表中选择一个选项

问题描述

这是我的错误:图片1

无论我选择什么选项,它都需要第一个..

选项:图片2

这是我的邮政编码:

if(isset($_POST['addtherun'])) {

  if ((isset($_POST['fiftychecked'])) && (isset($_POST['selectedoption']))) {



    $rowchauffeur = mysqli_query($conn, "SELECT * FROM taxi_drivers");

    $userinfo = mysqli_fetch_array($rowchauffeur);
    $id = mysqli_real_escape_string($conn, $userinfo['id']);
    $prenom = mysqli_real_escape_string($conn, $userinfo['prenom']);
    $nomdefamille = mysqli_real_escape_string($conn, $userinfo['nomdefamille']);
    $numerodevoiture = mysqli_real_escape_string($conn, $userinfo['numerodevoiture']);
    $selectedoption = mysqli_real_escape_string($conn, $_POST['selectedoption']);




    $insert1 = mysqli_query($conn, "INSERT INTO taxi_fiftyandplus (prenom, nomdefamille, numerodevoiture, datedecreation) VALUES ('$prenom', '$nomdefamille', '$numerodevoiture', now())") or die (mysqli_error($conn));

    echo 'fifty is checked<br>';
  } else if (isset($_POST['hundredchecked']))  {
    echo 'hundred is checked<br>';
  } else if (isset($_POST['twohundredchecked'])) {
    echo 'two hundred is checked<br>';
  } else {
    echo 'Select a length<br>';
  }

}

这是我的表格:

<form method="POST" action="index.php" >
          <div class="row">
              <div class="form-group col-md-3">
                <label>Chauffeur:</label>
                <!--  <div class="dropdown checkbox-menu allow-focus"> -->
      <!--  <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenu2" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dropdown</button> -->

';

$rowchauffeur = mysqli_query($conn, "SELECT * FROM taxi_drivers");


print "<div class='dropdown checkbox-menu allow-focus'>
<button class='btn btn-secondary dropdown-toggle' type='button' id='dropdownMenu2' data-toggle='dropdown' aria-haspopup='true' aria-expanded='false'>Dropdown</button>
<ul class='dropdown-menu checkbox-menu allow-focus'>
";
while( $row = mysqli_fetch_array($rowchauffeur))
print "

<li>
  <label> <input type='checkbox' class='sev_check2' name='selectedoption' > $row[id] $row[numerodevoiture] $row[prenom] $row[nomdefamille] </label>
  </li>
";
    print"  </ul></div>
</div>
</div>";





echo '


  <div class="form-check form-check-inline">
  <!-- <input type="hidden" name="fiftychecked" value="0"> -->
  <input type="checkbox" class="form-check-input sev_check" name="fiftychecked" id="s_fac">
  <label class="form-check-label" for="s_fac">50+</label>
</div>

<!-- Material inline 2 -->
<div class="form-check form-check-inline">
  <!-- <input type="hidden" name="twohundredchecked" value="0"> -->
    <input type="checkbox" class="form-check-input sev_check" name="hundredchecked" id="s_fac2">
    <label class="form-check-label" for="s_fac2">100+</label>
</div>

<!-- Material inline 3 -->

<div class="form-check form-check-inline">
  <!-- <input type="hidden" name="twohundredchecked" value="0"> -->
    <input type="checkbox" class="form-check-input sev_check" name="twohundredchecked" id="s_fac3">
    <label class="form-check-label" for="s_fac3">200+</label>
</div><br><br>

  <button type="submit" class="btn btn-primary" name="addtherun">Ajouter</button>
</form>

我期望的是我从下拉复选框列表中选择的值是应该插入到数据库中的实际值。

请不要介意凌乱的代码。

标签: phpmysql

解决方案


您必须将复选框命名为数组才能传递多个选择:

while( $row = mysqli_fetch_array($rowchauffeur))
print "
...
  <label> <input type='checkbox' class='sev_check2' name='selectedoption[]' > //<------- right here
  ...

推荐阅读