How to insert multiple values selected in checkbox in database

Total Post:10

Points:70
PHP 
PHP
 58247  View(s)
Ratings:
Rate this:
Dear all,

How to insert multiple values selected in checkbox in database.

Thanks
  1. Post:126

    Points:882
    Re: How to insert multiple values selected in checkbox in database

    Hi Pintu,
    If you are talking about group of checkbox with same name and you want to insert values of those checkbox which is checked, then you have to write following script to retrive values of those checkbox.

          <?php
        $checkBox = $_POST['things'];
        for($i=0; $i<sizeof($checkBox); $i++){
        $query = "INSERT INTO table(things) values('".$checkBox[$i]."')";
        mysql_query($query) or die(mysql_error());
        }
        ?>

    If this is not correct answer then please clarify your question that what you want to do with multiple checkbox in php.

      Modified On Apr-04-2018 01:06:39 AM
  1. Post:10

    Points:70
    Re: How to insert multiple values selected in checkbox in database

    Hello friend,

    I want to insert the values of the checkbox when checked and then click the button.After clicking the button all the checked checkbox values will be inserted into the database.

    My code

    form.php

    <html>
    <head>
    <title> PHP Form</title>
    </head>
    <body>
    <form action="checkbox.php" method="post">
    <input type="checkbox" name="chk1" value="a">a<br />
    <input type="checkbox" name="chk1" value="b">b<br />
    <input type="checkbox" name="chk1" value="c">c<br />
    <input type="checkbox" name="chk1" value="d">d<br />
    <br>
    <br>
    <input type="submit" name="Submit" value="Submit">
    </form>
    </body>
    </html>

    checkbox.php

    <?php
    session_start();
    include("config.php");
    $checkbox1 = $_POST['chk1'];
    if($_POST["Submit"]=="Submit")
    {
    for ($i=0; $i<sizeof($checkbox1);$i++) {
    $query="INSERT INTO check (type) VALUES ('".$checkbox1[$i]."')";
    mysql_query($query) or die ('Error updating database');
    echo "Record is inserted.";
    }
    }
    ?>

    config.php

    <?php
     $sDbHost = 'localhost';
     $sDbName = 'test';
     $sDbUser = 'root';
     $sDbPwd  = '';
    $dbConn = mysql_connect ($sDbHost, $sDbUser, $sDbPwd) or die ('MySQL connect failed. ' . mysql_error());
    mysql_select_db($sDbName,$dbConn) or die('Cannot select database. ' . mysql_error());
    ?>


    Please help me iam new to php.

    Thanks

      Modified On Apr-04-2018 01:06:01 AM
  1. Post:68

    Points:476
    Re: How to insert multiple values selected in checkbox in database

    Hi Pintu Nayak,
    In this code, you've done a little mistake in 'form.php' file. Replace your code with following code:
    'form.php' Code:

    <html>
    <head>
    <title> PHP Form</title>
    </head>
    <body>
    <form action="checkbox.php" method="post">
    <input type="checkbox" name="chk1[]" value="a">a<br />
    <input type="checkbox" name="chk1[]" value="b">b<br />
    <input type="checkbox" name="chk1[]" value="c">c<br />
    <input type="checkbox" name="chk1[]" value="d">d<br />
    <br>
    <br>
    <input type="submit" name="Submit" value="Submit">
    </form>
    </body>
    </html>

    Please marked as answer if this code will solve your problem.

      Modified On Apr-04-2018 01:05:08 AM
  1. Post:1

    Points:7
    Re: How to insert multiple values selected in checkbox in database

    hi, i wanna ask a question..i've got the same problem..help me please..what should i replace 'things' with? my DB column or what...

    <?php
        $checkBox = $_POST['things'];
        for($i=0; $i<sizeof($checkBox); $i++){
        $query = "INSERT INTO table(things) values('".$checkBox[$i]."')";
        mysql_query($query) or die(mysql_error());
        }
        ?>

      Modified On Apr-04-2018 01:04:11 AM
  1. Post:3

    Points:21
    Re: How to insert multiple values selected in checkbox in database

    $query= "INSERT INTO individual_skills(Skills_ID,Ind_ID) VALUES (" .$skills[$i]. ",".$_SESSION['Ind_ID'].")";
      for ($i=0; $i<sizeof($skills);$i++) {
     
      $query.= " VALUES (" .$skills[$i]. ",".$_SESSION['Ind_ID'].")";
      }

      Modified On Apr-04-2018 01:06:55 AM

Answer