douyuefei3546 2019-03-10 09:13
浏览 95
已采纳

多选不将所选选项插入数据库

This is PHP.

<?php
include 'connect.php';

if (isset($_POST['add'])) {
    $id = 'NULL';
    $Branch = $_POST['Branch'];
    $check = mysqli_query($connect, "SELECT * FROM staff WHERE id='$id'");
    if (0 == mysqli_num_rows($check)) {
        if (1 == 1) {
            $insert = mysqli_query($connect, "INSERT INTO staff(Branch) VALUES('$Branch')") or die(mysqli_error($connect));
            if ($insert) {
                echo '<div class="alert alert-success alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>Data Successfully Saved.</div>';
            } else {
                echo '<div class="alert alert-danger alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>OOps, Data Failed to Save!</div>';
            }
        } else {
            echo '<div class="alert alert-danger alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>Not the same password!</div>';
        }
    } else {
        echo '<div class="alert alert-danger alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>ID already exists!</div>';
    }
}
?>

This is my mysql column.

Branch  text    utf8_general_ci     No  None

This is select element from my form.

<form action="" method="post">
     <select class="selectpicker form-control" multiple data-live-search="true" name="Branch" required="true">
        <option selected>example 1</option>
        <option value="example 2">example 2</option>
        <option value="example 3">example 3</option>
        <option value="example 4">example 4</option>
      </select>
</form>

My problem is next, if i choose one option its indexing database without any problem. if i choose more then one option, last selected option is indexing. So what i want is if i select 2 option for example "example 1, example 2" this both should be index as text in my mysql.

P.S. I know that i am noob and non English speaker but please support me.

  • 写回答

3条回答 默认 最新

  • dongzhou1901 2019-03-10 09:40
    关注

    First of all, change attribute name like this name="Branch[]"

    next, use foreach loop and retrieve the value one by one

    $branches = $_POST['Branch']; //get values in array
    
    $insert_string = ''; //we need to concat array values into this empty variable
    

    use foreach loop

    foreach($branches as $branch){
       $insert_string .= $branch . ", "; //set values with comma separater
    }
    
    //insert
    $insert = mysqli_query($connect, "INSERT INTO staff(Branch) VALUES('$insert_string')") or 
                 die (mysqli_error($connect));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗