MySQL如何从复选框列表中选择一个选项

时间:2019-06-07 00:17:17

标签: php mysql

这是我的错误:Image1

无论我选择什么选项,它都会采用第一个选项。

选项:Image2

这是我的POST代码:

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>

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

请不要介意凌乱的代码。

3 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:-1)

通过此页面修复了该问题:getting a checkbox array value from POST

和这里所有尝试过的人。

这是我的解决方法:

  if(isset($_POST['selectedoption'])){
  if (is_array($_POST['selectedoption'])) {
    foreach($_POST['selectedoption'] as $value){
      if (isset($_POST['fiftychecked'])) {



      $rowchauffeur = mysqli_query($conn, "SELECT * FROM taxi_drivers WHERE id='$value'");

      $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']);


      $insert1 = mysqli_query($conn, "INSERT INTO taxi_fiftyandplus (prenom, nomdefamille, numerodevoiture, datedecreation) VALUES ('$prenom', '$nomdefamille', '$numerodevoiture', now())") or die (mysqli_error($conn));
echo $value;
      echo 'fifty is checked<br>';
      }
    }
  }
}

答案 2 :(得分:-1)

您可以尝试这个:)

<?php
if(isset($_POST['submit'])){

for($i = 0; $i < count($_POST['selectedoption']); $i++){
    $selectedoption = mysqli_real_escape_string($conn, $_POST['selectedoption'][$i]);

$query = mysqli_query($conn,"SELECT * FROM taxi_drivers WHERE id='$selectedoption' ")or die(mysqli_error($conn));
$row = mysqli_fetch_array($query);

$id = $row['id'];   
$prenom = $row['prenom'];
$nomdefamille = $row['nomdefamille'];
$numerodevoiture = $row['numerodevoiture'];


$insert = mysqli_query($conn,"INSERT INTO taxi_fiftyandplus(prenom, nomdefamille, numerodevoiture, datedecreation)VALUES('$prenom','$nomdefamille','$numerodevoiture',NOW() ) ")or die(mysqli_error($conn));
}
}
?>

<form method="POST" action="" >
        <div class="row">
          <div class="form-group col-md-3">
            <label>Chauffeur:</label>


<?php
$rowchauffeur = mysqli_query($conn, "SELECT * FROM taxi_drivers ORDER BY id ASC");
?>

                <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'>

<?php
while($row = mysqli_fetch_array($rowchauffeur)){
$id = $row['id'];   
$prenom = $row['prenom'];
$nomdefamille = $row['nomdefamille'];
$numerodevoiture = $row['numerodevoiture'];
?>
                            <li>
                                <label> <input type='checkbox' class='sev_check2' name='selectedoption[]' value="<?php $id; ?>"><?php $id." - ".$prenom." - ".$nomdefamille; ?></label>
                            </li>
<?php
}
?>

                        </ul>

                </div>
            </div>
        </div>

        <input type="submit" name="submit" value="Submit">
</form>