【问题标题】:Multiple select not inserting selected options to database多选未将所选选项插入数据库
【发布时间】:2019-07-31 20:09:02
【问题描述】:

这是 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>';
    }
}
?>

这是我的mysql专栏。

Branch  text    utf8_general_ci     No  None

这是我表单中的选择元素。

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

接下来是我的问题, 如果我选择一个选项,它的索引数据库没有任何问题。 如果我选择多个选项,则最后选择的选项是索引。 所以我想要的是,如果我选择 2 选项,例如“示例 1,示例 2”,这两个选项都应该在我的 mysql 中作为文本索引。

附:我知道我是菜鸟,不会说英语,但请支持我。

【问题讨论】:

  • 你能添加一些PHP代码吗?您到底想在“分支”列中插入什么?像“示例 1,示例 2”这样的文本?
  • 你是如何连接到数据库的?
  • 我添加了我的 php 代码
  • 我不懂 SELECT

标签: php mysql


【解决方案1】:

首先,将属性名改成这样name="Branch[]"

接下来,使用foreach循环,逐一检索值

$branches = $_POST['Branch']; //get values in array

$insert_string = ''; //we need to concat array values into this empty variable

使用foreach循环

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

【讨论】:

  • 他/她不想插入多条记录,而是插入一条连接字符串的记录。
  • 你是天才,谢谢我的伙伴
  • 您可以使用implode(),而不是使用foreach 循环来连接字符串,它不会在末尾添加额外的逗号。看我的回答。
【解决方案2】:

&lt;select&gt; 元素的名称应为 Branch[]。在您的 PHP 脚本中,您需要在将 $branches 插入数据库之前使用 $branches = implode(',', $_POST['Branch']); 获取值。

【讨论】:

    【解决方案3】:

    您需要给它一个数组样式的名称,name="Branch[]"。然后$_POST['Branch'] 将是一个包含所有选定值的数组,您可以循环插入其中的所有值。

    if (isset($_POST['Branch'])) {
        foreach ($_POST['Branch'] as $branch) {
            // insert $branch into the database
        }
    }
    

    如果要插入逗号分隔的字符串,可以使用implode

    $branches = implode(',', $_POST['Branch']);
    

    但是将多个项目放在单个列中通常是糟糕的数据库设计。见Is storing a delimited list in a database column really that bad?

    另外,在 HTML 中,required="true" 应该是 requiredrequired="required"。布尔属性不使用true/false 值;它们要么没有值,要么与属性名称相同,要么被省略。

    【讨论】:

    • 我尝试了这个但没有任何改变,它的索引文本“数组”
    • 我的意思是“数组”文本正在数据库中建立索引。
    • 这意味着您没有在数组上循环。如果您尝试将数组用作字符串,它将变成单词Array
    • 我认为当我说“您可以在循环中插入所有值”时会很清楚,但我添加了示例代码以使其更清晰。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-28
    • 2023-02-03
    相关资源
    最近更新 更多