【问题标题】:how to select in database all selected in multiple select in php?如何在数据库中选择所有在php中的多项选择中选择的?
【发布时间】:2018-08-18 18:15:04
【问题描述】:

这是我的过程

  $sec_id = implode(', ', $_POST['sec_id']);

  $stmtSec = $crud->runQuery("SELECT * FROM tbl_section WHERE sec_id IN (':sec_id')");
  $stmtSec->execute(array(":sec_id" => $sec_id));
  while ($rowSec = $stmtSec->fetch(PDO::FETCH_ASSOC))
  {
     $section[] = $rowSec['section'];
  }

但是使用我的代码没有任何显示。

这是我的多选代码。

                      <select name="sec_id[]" data-live-search="true" class="selectpicker form-control border-input" style="text-align-last:center;" title="Select Section" required multiple>
                        <?php
                        $stmtSection = $crud->runQuery('SELECT * FROM tbl_section ORDER BY sec_id DESC');
                        $stmtSection->execute();
                        while($rowSection=$stmtSection->fetch(PDO::FETCH_ASSOC))
                        {
                          print("<option value='".$rowSection['sec_id']."'>".$rowSection['section']."</option>");
                        }
                        ?>
</select>

还有另一种在数据库中选择多个的方法吗?或查询? 请帮忙。

【问题讨论】:

  • also: implode(', ', $rowSec['section']) 暗示 tbl_section .section 是一个数组,这在关系数据库中是一件非常奇怪的事情
  • 你能举个例子吗

标签: php html mysql pdo


【解决方案1】:

这里我已经包含并更新了代码。

    $sec_id = implode(', ', $_POST['sec_id']);
    $stmtSec = $crud->runQuery("SELECT * FROM tbl_section WHERE sec_id IN (':sec_id')");
    $stmtSec->execute(array(":sec_id" => $sec_id));
    while ($rowSec = $stmtSec->fetch(PDO::FETCH_ASSOC)) {
       $section[] = $rowSec['sec_id'];
    }
    $stmtSection = $crud->runQuery('SELECT * FROM tbl_section ORDER BY sec_id DESC');
    $stmtSection->execute();
    while($rowSection=$stmtSection->fetch(PDO::FETCH_ASSOC))
    {
        $optionsSelected = in_array($rowSection['sec_id'], $section) ? 'selected="selected"' : '';
        print("<option'" . $optionsSelected . "'value='".$rowSection['sec_id']."'>".$rowSection['section']."</option>");
    }

【讨论】:

  • 请再次查看我的查询。我更新了问题。
猜你喜欢
  • 2019-09-30
  • 2021-12-06
  • 2014-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-23
  • 2012-04-13
  • 1970-01-01
相关资源
最近更新 更多