title: PHP: How to get multiple checkbox values from database for same ID?
body: <p>I am new to php &amp; need little help here.
  I've following code to INSERT multiple checkbox values into a table for same ID:</p>
  
  <p>on my form I have:</p>
  
  <pre><code>&lt;input type="checkbox" name="subcat[]" value="Mech"&gt;Mech
  &lt;input type="checkbox" name="subcat[]" value="Ele"&gt;Ele
  &lt;input type="checkbox" name="subcat[]" value="Civ"&gt;Civ
  &lt;input type="checkbox" name="subcat[]" value="Air"&gt;Air
  &lt;input type="checkbox" name="subcat[]" value="BSL"&gt;BSL
  </code></pre>
  
  <p>I've checked "Mech", "Ele" and "BSL"</p>
  
  <p>and PHP to insert:</p>
  
  <pre><code>$subcat = $_POST['subcat'];
      for($i=0; $i&lt;sizeof($subcat);$i++){
      $sql1="INSERT INTO mst_scatadd (party_code,scid)
      VALUES ('$pcode',$subcat[$i])";
      mysql_query($sql1,$con);
      }
  </code></pre>
  
  <p>db looks something like...</p>
  
  <pre><code>sctid  | party_code  |   scid
  ---------------------------------
    1    |   01S001    |    Mech
    2    |   01S001    |    Ele
    3    |   01S001    |    BSL
    4    |   01K207    |    Main
  </code></pre>
  
  <p>Now, how can I retrieve these values from db to my form for the same ID and make them as checked if I want to edit the checkboxes?</p>
  
  <p>Little help would be appreciated!!</p>
  
  <p>Thanks in adv. </p>
  
  <h2>EDIT 1:</h2>
  
  <p>Hello again guys!</p>
  
  <p>I've done with inserting and retrieving checkbox values using implode and explode and its working fine.
  Thanks to @Antoniossss for good suggession to keep checkboxes values as numbers(make sure the column in db should be VARCHAR)
  Thanks to @Barmar to explain use of explode &amp; thanks to @MarkTWebsite to share idea of if-else</p>
  
  <p>Code for inserting multiple checkbox values:</p>
  
  <pre><code>foreach ($_POST['subcat'] as $_subcat)
      {
          $checksub[] = $_subcat;
      }   $finalsub = implode(',', $checksub);
  
  $sql="INSERT INTO vendor_mst(party_code, subcat) VALUES ('$pcode','$finalsub')";
  </code></pre>
  
  <p>My final code to retrieve checkbox values from db...</p>
  
  <pre><code>sql="SELECT * FROM vendor_mst WHERE party_code like '".$searchname."'";
  $result=mysql_query($sql,$con);
  while ($row=mysql_fetch_array($result, MYSQL_BOTH)){
  $checksub  = explode(',',$row['subcat']);
  }
  if(in_array("1",$checksub))echo '&lt;input type="checkbox" name="subcat[]" value="1" checked &gt;Mech'; else echo '&lt;input type="checkbox" name="subcat[]" value="1"&gt;Mech';
  if(in_array("2",$checksub))echo '&lt;input type="checkbox" name="subcat[]" value="2" checked &gt;Ele';  else echo '&lt;input type="checkbox" name="subcat[]" value="2"&gt;Ele';
  if(in_array("3",$checksub))echo '&lt;input type="checkbox" name="subcat[]" value="3" checked &gt;Civ';  else echo '&lt;input type="checkbox" name="subcat[]" value="3"&gt;Civ';
  if(in_array("4",$checksub))echo '&lt;input type="checkbox" name="subcat[]" value="4" checked &gt;Air';  else echo '&lt;input type="checkbox" name="subcat[]" value="4"&gt;Air';
  if(in_array("5",$checksub))echo '&lt;input type="checkbox" name="subcat[]" value="5" checked &gt;BSL';  else echo '&lt;input type="checkbox" name="subcat[]" value="5"&gt;BSL';
  </code></pre>
  
  <p>Thanks to stackoverflow!</p>