0

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.

pmatsumura
  • 391
  • 1
  • 6

3 Answers3

0

You need to give it an array-style name, name="Branch[]". Then $_POST['Branch'] will be an array containing all the selected values, and you can insert all the values from it in a loop.

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

If you want to insert a comma-delimited string you can use implode:

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

But it's geneally poor database design to put multiple items in a single column. See Is storing a delimited list in a database column really that bad?

Also, in the HTML, required="true" should be either required or required="required". Boolean attributes don't use true/false values; they either have no value, the value is the same as the attribute name, or they're omitted.

Barmar
  • 741,623
  • 53
  • 500
  • 612
0

The name of the <select> element should be Branch[]. In your PHP script you'll need to get the value with $branches = implode(',', $_POST['Branch']); before inserting $branches into the database.

pmatsumura
  • 391
  • 1
  • 6
0

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));
Arsee
  • 283
  • 5
  • 20