简体   繁体   English

如何使用Bootstrap选择框和PHP准备的语句将多行插入Mysql表?

[英]How to insert multiple rows into Mysql table using Bootstrap select boxes and PHP prepared statements?

I'm trying to insert multiples rows into MySql, using PHP prepared Statements and Boostrap Select Picker. 我正在尝试使用PHP准备的Statements和Boostrap Select Picker将多个行插入MySql。 I tried to research some questions here but unfortunately i couldn't solve this code problem. 我试图在这里研究一些问题,但不幸的是我无法解决此代码问题。 Below are the questions that i tried to solve my doubt but without success: 以下是我试图解决我的疑问但未成功的问题:

  1. multiple row insertion in mysql table through php 通过php在mysql表中插入多行

  2. Inserting multiple rows into some table using values from select statement 使用select语句的值将多行插入某个表

  3. Multiple row insert to table if check box is selected 如果选中了复选框,则将多行插入表格

  4. How to insert multiple table rows into database using php 如何使用php将多个表行插入数据库

  5. Inserting multiple rows into MySql with one INSERT statement using PHP implode function 使用PHP implode函数通过一条INSERT语句将多行插入MySql

Just for resume my problem, i have two MySql tables that call: 为了恢复我的问题,我有两个调用的MySql表:

(tbl_colors) (tbl_colors)

Collunm: ColorID - Primary Key - AI
Collunm: color - Varchar - 100

colorID|color
-------------
01     |White
02     |Black
03     |Blue

and (tbl_colors_options) (tbl_colors_options)

Collunm: ID - Primary Key - AI
Collunm: colorFK - INT 11 - INDEX - ForeingKey from **(tbl_colors)**

ID |colorFK| 
------------
01 |  1 
02 |  3
03 |  2 
03 |  2 
03 |  3 
03 |  3  

After i created that 2 tables above into MySql, i created a page with a dropdown list that display all values from (tbl_colors) . 在MySql中创建了上述2个表之后,创建了一个带有下拉列表的页面,该页面显示(tbl_colors)中的所有值。 I used a Bootstrap Multiple Select Boxes from this website here https://developer.snapappointments.com/bootstrap-select/examples/ to display on page. 我在此网站的https://developer.snapappointments.com/bootstrap-select/examples/上使用了Bootstrap Multiple Select Boxs在页面上显示。 Into this select box i have this values from (tbl_colors) : 1 , 2 and 3 where i can select all of them or some of them to insert that values through submit button to (tbl_colors_options) . 这个选择框我从(tbl_colors)这个值:1,23在那里我可以选择所有这些或其中一些的插入,价值观通过提交按钮(tbl_colors_options)。

Below i show the code that i'm using to insert multiple row on (tbl_colors_options) : 下面我显示了我用来在(tbl_colors_options)上插入多行的代码:

<!-- Jquery and Bootstrap 4 CDN's -->

<script src="https://code.jquery.com/jquery-3.4.0.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" />

<!-- Bootstrap Select CSS Box-->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-select@1.13.9/dist/css/bootstrap-select.min.css">

<!-- Bootstrap Select JS -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap-select@1.13.9/dist/js/bootstrap-select.min.js"></script>

and HTML and PHP code that display the dropdown list (Bootstrap Multiple Select box): 以及显示下拉列表的HTML和PHP代码(“ Bootstrap Multiple Select”框):


<div class="container">
<br>
<form method="post" id="option_form" action="add.php">
<div class="form-row">
<div class="col-md-8 mb-3" >

<label><b>Options to select</b></label>


<?php

include 'db.php';
$stmt = $connection->prepare('SELECT * FROM tbl_colors');
$stmt->execute();
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>

<select name="colorFK" id="colorFK" class="form-control selectpicker" multiple>
<?php foreach($results as $row): ?>
<option value="<?= $row['colorID']; ?>"><?= $row['colorID']; ?></option>    
<?php endforeach ?>
</select> 


</div>
</div>
<div class="form-row">
<div class="col-md-8 mb-3" align="right">
<input type="submit" name="submitbtn" id="submitbtn" class="btn btn-success" value="Add Options" />
</div>
</div>

</form>

</div>

below is my DB connection PHP file (db.php): 以下是我的数据库连接PHP文件(db.php):

<?php

$username = 'root';
$password = '';
$connection = new PDO( 'mysql:host=localhost;dbname=system;charset=utf8;', $username, $password );

?>

and finally, below is the add.php file that i'm using to insert row into (tbl_colors_options) : 最后,下面是我用来将行插入(tbl_colors_options)add.php文件:

<?php

include 'db.php';

if(isset($_POST["submitbtn"]))
{
    if($_POST["submitbtn"])
    {

        $statement = $connection->prepare("
            INSERT INTO tbl_colors_option (colorFK) 
            VALUES (:colorFK)
        ");

        $result = $statement->execute(
            array(
                ':colorFK'  =>  $_POST["colorFK"],
            )
        );

        if(!empty($result))
        {
            echo 'option successfully added!';
        }

}}
?>

The add.php code above works fine and just insert one row into tbl_colors_option . 上面的add.php代码工作正常,只需将一行插入tbl_colors_option即可 How can i improve the add.php code to insert multiple rows that i selected on Bootstrap Multiple Select Box? 如何改善add.php代码以插入在Bootstrap Multiple Select Box上选择的多行?

I found a solution for this doubt. 我为这个疑问找到了解决方案。 I modified the add.php file with the PHP PDO code below and now the code below insert multiple records into MySql table based on values selected in Boostrap Multiple Selected Boxes and now works fine: 我使用下面的PHP PDO代码修改了add.php文件,现在下面的代码根据Boostrap Multiple Selected Boxes中选择的值将多个记录插入MySql表中,现在可以正常工作:

<?php
    $db_host = "localhost";
    $db_name = "system";
    $db_user = "root";
    $db_pass = "";

    try{
        $db_con = new PDO("mysql:host={$db_host};dbname={$db_name}",$db_user,$db_pass);
        $db_con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e){
    echo $e->getMessage();
}

try{

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

    $multi=$_POST['colorFK'];

foreach ($multi as $multir){

$stmt=$db_con->prepare("INSERT INTO tbl_colors_options (colorFK) VALUES(:colorFK)");
$stmt->bindParam(":colorFK", $multir);
$stmt->execute();

}
}
}
    catch(PDOException $e){
    echo $e->getMessage();

}

?>

If someone has some good improve for this code above it will be nice :) 如果有人对上面的代码有一些好的改进,那就太好了:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM