HOW TO INSERT MULTIPLE VALUES SELECTED IN CHECKBOX IN DATABASE

pintu nayak

Total Post:10

Points:70
Posted by  pintu nayak
PHP 
PHP
 55846  View(s)
Ratings:
Rate this:
Dear all,

How to insert multiple values selected in checkbox in database.

Thanks
  1. Awadhendra Tiwari

    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.

  1. pintu nayak

    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

  1. Arun Singh

    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 Mar-29-2012 03:48:26 AM
  1. amy ya

    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());
        }
        ?>

  1. Phagu MAhato

    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'].")";
      }

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!