如何在php中插入多个选定的数据?

时间:2018-03-10 10:19:35

标签: php mysql pdo

我试图在数据库中选择多个数据并插入多个数据。 这是我的查询

多重选择:

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

多选到数据库:

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

向数据库插入多个数据:(我收到错误的地方)

$query="INSERT INTO tbl_login(username,password,access_type,section,sec_id) 
        VALUES(:username, :password, :access_type, :section, :sec_id)";
if($crud->addAccount($account,$new_password,$access,$query,$section,$sec_id))

功能:

    public function addAccount($account,$new_password,$access,$query,$section,$sec_id)
    {
      $stmt = $this->conn->prepare($query);
      $stmt->execute(array(":username"=>$account,
                           ":password"=>$new_password,
                           ":access_type"=>$access,
                           ":section"=>$section,
                           ":sec_id"=>$sec_id));

      return $stmt;
    }

我遇到插入问题。 请帮我检查一下我的查询。提前谢谢。

0 个答案:

没有答案