Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Jan 2011
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    how to insert multiple checkbox value in mysql database

    how to insert multiple checkbox value in mysql database in php and what sould be Databse structre for this form.

    Please help me Sir
    Thank you in advance



    This is my PHP code
    --------------------------------------------------------------
    PHP Code:
    <?php
    include('include/config.php');
    if(isset(
    $_POST["submit"]))
    {
    $name=$_POST['name'];
    $mail=$_POST['email'];
    $colour=$_POST['color'];
    $gen=$_POST['gender'];


    mysql_query("Insert into user values('','$name','$mail','$colour','$gen')") or die("Data Not Inserted");
     
    }
    else
    {
    echo 
    "Please select Submit Button";

    }


    ?>
    -------------------------------------------------------------------------------------------------



    This is my html From
    ------------------------------------------------------------------------------------------
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>registration</title>
    
    
    </head>
    
    <body><form action="add_user.php" method="post" name="form" enctype="multipart/form-data">
    <h3 align="center">Registration Form</h3>
    <table width="400" height="503" border="0" align="center" >
    
      <tr>
        <td width="135" height="59">Name:</td>
        <td width="255"><label>
          <input type="text" name="name" id="name" />
        </label></td>
      </tr>
      
      <tr>
        <td>Email:</td>
        <td><label>
          <input type="text" name="email" id="email" />
        </label></td>
      </tr>
      <tr>
        <td>Colours</td>
        <td><label>
          <input type="checkbox" name="color[]" id="color" value="Blue" />Blue<br />
          <input type="checkbox" name="color[]" id="color" value="Red" />Red<br />
          <input type="checkbox" name="color[]" id="color" value="Green" />Green<br />
          <input type="checkbox" name="color[]" id="color" value="Yellow" />Yellow<br />
        </label></td>
      </tr>
      <tr>
        <td>Gender:</td>
        <td><label>
          <input type="radio" name="gender" id="gender" value="Male" />Male
          <input type="radio" name="gender" id="gender" value="Female" />Female
        </label></td>
      </tr>
      
      <tr>
        <td height="41">&nbsp;</td>
        <td><label>
        <input type="submit" name="submit" id="submit"  value="submit"  />
        </label></td>
      </tr>
    </table>
    </form>
    </body>
    </html>
    -------------------------------------------------------------------------------------------------------

    Please Reply Me Sir

    Thank you
    Last edited by Lamped; 01-29-2011 at 11:46 AM. Reason: mistak in name | Added php/code tags - Lamped

  • #2
    Regular Coder
    Join Date
    Apr 2007
    Posts
    317
    Thanks
    24
    Thanked 3 Times in 3 Posts
    This should work
    Quote Originally Posted by phpfrnd View Post
    PHP Code:
    mysql_query("INSERT INTO user ('name', 'mail', 'colour', 'gen') VALUES ('" $_POST['name'] . "', '" $_POST['mail'] . "', '" $_POST['colour'] . "', '" $_POST['gen'] . "')") or die("Data Not Inserted"); 

  • #3
    New Coder
    Join Date
    Jan 2011
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts
    its not working-it Display data not inserted

    mysql_query("INSERT INTO user ('name', 'mail', 'colour', 'gen') VALUES ('" . $_POST['name'] . "', '" . $_POST['mail'] . "', '" . $_POST['colour'] . "', '" . $_POST['gen'] . "')") or die("Data Not Inserted");

  • #4
    New Coder
    Join Date
    Jan 2011
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts
    how to insert Multiple checkbox value into single column of mysql database in php
    Last edited by phpfrnd; 02-02-2011 at 10:21 AM. Reason: editing

  • #5
    Regular Coder
    Join Date
    Apr 2007
    Posts
    317
    Thanks
    24
    Thanked 3 Times in 3 Posts
    Just remove the single quote from the query at the field names. If that does not work for you then check your field type in your sql table. If the field type is an integer then you need to remove the quotes from the values side as well. If the fields are varchar it should be fine.
    PHP Code:
    mysql_query("INSERT INTO user (name, mail, colour, gen) VALUES ('" $_POST['name'] . "', '" $_POST['mail'] . "', '" $_POST['colour'] . "', '" $_POST['gen'] . "')") or die("Data Not Inserted"); 

  • #6
    New Coder
    Join Date
    Jan 2011
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hello dear
    macleodjb

    all fields are working but in mysql dabase 'colour 'field storing only 'array' not all colour.

    mysql_query("INSERT INTO user (name, mail, colour, gen) VALUES ('" . $_POST['name'] . "', '" . $_POST['mail'] . "', '" . $_POST['colour'] . "', '" . $_POST['gen'] . "')") or die("Data Not Inserted");



    please reply me

  • #7
    Regular Coder
    Join Date
    Apr 2007
    Posts
    317
    Thanks
    24
    Thanked 3 Times in 3 Posts
    Quote Originally Posted by phpfrnd View Post
    hello dear
    macleodjb

    all fields are working but in mysql dabase 'colour 'field storing only 'array' not all colour.

    mysql_query("INSERT INTO user (name, mail, colour, gen) VALUES ('" . $_POST['name'] . "', '" . $_POST['mail'] . "', '" . $_POST['colour'] . "', '" . $_POST['gen'] . "')") or die("Data Not Inserted");



    please reply me
    ok, then you will have to take the array and create a string before you insert it into your table.

    PHP Code:
    $colors $_POST['color'];
    $selected_colors "";
    foreach (
    $colors as $color) {
    $selected_colors .= $color ", ";
    }
    $selected_colors substr($selected_colors0, -2);

    mysql_query("INSERT INTO user (name, mail, colour, gen) VALUES ('" $_POST['name'] . "', '" $_POST['mail'] . "', '" $selected_colors "', '" $_POST['gen'] . "')") or die("Data Not Inserted"); 


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •