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 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)