title: Inserting values from multiple checkboxes into multiple rows in a mysql database using php
body: <p>I have stayed up two nights and I haven't been able to fix this. I am new to the site as well as in <strong>PHP</strong> please forgive my inexperience. The idea is that when a user selects several courses it should be sent to the database and stored in separate rows. what happens now is that it stores only the first value twice in the database. thanks.
  code:</p>
  
  <pre><code>&lt;?php
  include 'core/init.php';
  protect_page();
  include 'includes/overall/header.php';
  $user_id=$_SESSION['user_id'];
  ?&gt;
  
  &lt;h2&gt;Register&lt;/h2&gt;
  
  &lt;?php
  
  if(isset($_GET['success']) &amp;&amp; empty($_GET['success'])){
      echo 'You have successfully registered!';
  }
  else{
          if(empty($_POST)===false){
          $course[]=$_POST['course_code'];
          $user_id= $user_data['user_id'];
          $username=$user_data['username'];
  
          foreach($course as $c){
  
          $data= '\''.implode('\',\'',$c).'\'';
  
          mysql_query("INSERT INTO `lenroc_ssims`.`registercourses`(`user_id`, `username`, `course_code`) VALUE ('$user_id','$username', $data)");
  
              header('location:courses.php?success');
              exit();
              }
          }
      ?&gt;
      &lt;form action="" method="post"&gt;
      &lt;?php
  
      $sql = "SELECT * FROM course";
      $result = mysql_query($sql)or die(mysql_error());
  
      echo "&lt;table&gt;";
      echo "&lt;tr&gt;&lt;th&gt;COURSE CODE&lt;/th&gt;&lt;th&gt;COURSE TITLE&lt;/th&gt;&lt;th&gt;UNIT&lt;/th&gt;&lt;th&gt;SEMESTER&lt;/th&gt;&lt;th&gt;LEVEL&lt;/th&gt;&lt;/tr&gt;";
  
      while($row = mysql_fetch_array($result)){
      $course_code        = $row['course_code'];
      $course_title       = $row['course_title'];
      $course_unit        = $row['course_unit'];
      $semester           = $row['semester'];
      $level              = $row['level'];
      echo "&lt;tr&gt;&lt;td style='width: 100px;'&gt;".$course_code."&lt;/td&gt;&lt;td style='width: 600px;'&gt;".$course_title."&lt;/td&gt;&lt;td&gt;".$course_unit."&lt;/td&gt;&lt;td&gt;".$semester."&lt;/td&gt;&lt;td&gt;".$level."&lt;/td&gt;&lt;td&gt;&lt;input type=\"checkbox\" name=\"course_code[]\" value=".$course_code."&gt;&lt;/td&gt;&lt;/tr&gt;";
      } // End our while loop
      echo "&lt;/table&gt;";
      ?&gt;
      &lt;input type="submit" value="Register"&gt;
      &lt;/form&gt;
  
  &lt;?php
  }
  include 'includes/overall/footer.php';
  ?&gt;
  </code></pre>