save multiple values of checkbox in database


i am trying to save multiple values from checkbox in database (i.e different rows) from user end form. code for my form is

<form action="a_insert_vendor_sector.php" method="post"> <?php $sql = "SELECT * FROM sector where cityid='".$cityid."'"; $sql1 = mysqli_query($con, $sql); if (mysqli_num_rows($sql1) > 0) { while($row = mysqli_fetch_assoc($sql1)) { $sector = $row["sector"]; $sectorid = $row["id"]; echo $sector; echo "<input type='checkbox' name='sectorid[]' value='$sector' >"; echo "<br>"; } } ?> <footer> <div class="submit_link"> <input type="submit" name="submit" value="Submit" class="alt_btn"> </div> </footer> </form>

backend code is

<?php include('a_session.php'); require 'connection.php'; $sectorid = implode(' ', $_POST['sectorid']); if(isset($_POST['submit'])) { $sql="INSERT INTO vendor_sector(sectorid) VALUES ('$sectorid')"; if (mysqli_query($con, $sql)) { echo "success"; } else { echo "Error updating record: " . mysqli_error($con); } } ?>

Original table view vendor_sector

id sectorid 1 A

When i submit the form, i get the value in form of array like this

Array ( [0] => A [1] => B )

so the values are being carried properly till back end, but i am not able to save them properly.

My problem is that if i select 2 values e.g A,B they are getting saved as one value like this

id sectorid 1 A,B

Whereas i wish to save the value as

id sectorid 1 A 2 B


Build the query like -

Implode them with brackets () -

$sectorid = "('" . implode("') , ('", $_POST['sectorid']) . "')";

Then pass it to the query -

$sql="INSERT INTO vendor_sector(sectorid) VALUES $sectorid";

Also you need to put the implode inside the $_POST check as if it is not posted then you will get error.


Try this:

<?php include('a_session.php'); require 'connection.php'; $sectorids = $_POST['sectorid']; if(isset($_POST['sectorid'])) { foreach($sectorids as $sectorid){ $sql="INSERT INTO vendor_sector(sectorid) VALUES ('$sectorid')"; if (mysqli_query($con, $sql)) { echo "success"; } else { echo "Error updating record: " . mysqli_error($con); } } } } ?>


  • Query returned as Boolean?
  • Display all rows and update all rows by a submit
  • PHP not displaying result from MYSQL query
  • mysqli_num_rows() expects parameter 1 to be mysqli_result, array given
  • change a key of a single element array
  • MySQL not pulling all rows
  • Problems displaying blob images
  • explode results then implode php
  • Check for same rows in a while loop and put them in a separate table
  • Displaying results hyperlink numbers like a search engine
  • How to generate dynamic dropdown PHP Javascript mySQL
  • Warning: mysqli_real_escape_string() expects exactly 2 parameters
  • jquery ajax dont work without firebug break point
  • Passing data-* to bootstrap Modal
  • PHP & MySQL: mysqli_num_rows() error. What is wrong? [duplicate]
  • Create a list like array when echoed in php
  • How to retrieve data from multiple tables using a PHP form?
  • PHP Database driven Multilevel Menu
  • PHP & MySQL - list query results under heading and subheading
  • PHP + MySQL - Autocomplete from database not getting data from table
  • Imports in __init__.py and `import as` statement
  • Escaping single quotes in JDBC with MySql
  • Adding Parent and Child Nodes in TreeView from Sql Server 2008
  • Stop Bash Script if Hive Fails
  • How to add git credentials to the build so it would be able to be used within a shell code?
  • FFmpeg Conversion Error
  • Java Scanner input dilemma. Automatically inputs without allowing user to type
  • Getting last autonumber in access
  • what is the difference between the asp.net mvc application and asp.net web application
  • Display Images one by one with next and previous functionality
  • Upload files with Ajax and Jquery
  • Matrix multiplication with MKL
  • How to delete a row from a dynamic generate table using jquery?
  • json Serialization in asp
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • How do I configure my settings file to work with unit tests?
  • How to stop GridView from loading again when I press back button?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Binding checkboxes to object values in AngularJs