How To Insert Multiple Check Box Values into MySQL Database ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ROXIT
    New Member
    • Apr 2013
    • 10

    How To Insert Multiple Check Box Values into MySQL Database ?

    hello every one pls tell me how can i save multiple checkbox value into database.

    I have created one table into the database named Category_Table and another table named CategoryTitle.
    for e.x.

    1.Graphics and Animation(From Category_Table)

    1.Concept Artist(From CategoryTitle Table)
    2.Animator
    3.Stereographer
    4.Compositor
    2.Sound

    1.Sound Assistant
    2.Sound Engineer

    So what i am doing here is m calling these values from the database in a checkbox.

    Code:
    while($category_id <= 30)
    	{
    	$result = mysql_query("SELECT * FROM category_tbl where category_id = $category_id ");
    	$row = mysql_fetch_assoc($result);
    	echo '<div id="main">';
    	echo '<div class="content">';
    	echo '<div class="title">';
    	echo $row['category'];
    	echo '</div>';
    	echo '<div class="image"><button onclick="toggle5(showDiv'.$category_id.')" ><strong>Expand</strong></button>';
    	echo '</div>';
    	echo '<div id="showDiv'.$category_id.'" style="display:block">';
    	
    		$cat_title = mysql_query("SELECT * FROM category_title_tbl WHERE category_id = '$category_id'") or die (mysql_error()); 
                while ($cat_tit=mysql_fetch_array($cat_title))
    			{?>             
                 <input type="checkbox" value="$cat_tit['category_title']" id="cat_title" />
                 <?php echo $cat_tit['category_title'];
    			 echo '<br />';
                
    			}   
    	echo '</div>';
    	echo '</div>';
    	echo '</div>';
    	$category_id = $category_id + 1;
    	}
    so what i want is when user select multiple checkboxes then those values get insert into the database...

    pls tell me how can i do this...

    plsss rply..
  • Ammu
    New Member
    • Aug 2011
    • 78

    #2
    Code:
     <input type="checkbox" value="<? echo $cat_tit['category_title']; ?>" id="cat_title" name="cat_title[]" />
    and you can take values
    Code:
    if(isset($_POST['submit']))
    {
      for($i=0;$i<=count($_POST['cat_title']);$i++)
    
       {
        echo $_POST['cat_title'][$i];
       }
    }

    Comment

    • ROXIT
      New Member
      • Apr 2013
      • 10

      #3
      hey thanks ammu its working but how can i collect those values to an array and insert into database.

      Comment

      • Ammu
        New Member
        • Aug 2011
        • 78

        #4
        Code:
        $x = array();
        for($i=0;$i<=count($_POST['cat_title']);$i++) 
           {
            $x .= $_POST['cat_title'][$i]."|";
           }
        print_r($x);
        You will get a string like category_title1 |category_title 2|category_titl e3|............ .

        Comment

        Working...