title: how to insert data into database through foreach in php
body: <p>I have to insert data into <code>databases</code> through <code>foreach</code>, but it is not working...</p>
  
  <p><img src="https://i.stack.imgur.com/93Hzz.png" alt="snapshot of form">
      
      $a=mysql_query("select * from subject where batch='$cl'");
      if(mysql_num_rows($a)>0) {
          $result=mysql_fetch_array($a)
      ?>
      
  
  <pre><code>$exp_array=explode(',',$result[3]);
  foreach($exp_array as $vl) {
  ?&gt;
      &lt;div class="row"&gt;
          &lt;div class="col-sm-6 pad-left"&gt;  
              &lt;?php echo '&lt;br/&gt;'; echo $vl; ?&gt;
          &lt;/div&gt; 
          &lt;div class="col-sm-6 pad-left"&gt; 
              &lt;input  name="exam_date[]" type="text" placeholder="&lt;?php echo $vl;?&gt;" class="form-control"&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;?php } ?&gt;
  &lt;br/&gt;
  &lt;button type="submit" class="btn btn-success col-sm-offset-4"&gt;Submit&lt;/button&gt;
  
  &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>This code is to insert data dynamically from database which is stored from another table.</p>
  
  <pre><code>&lt;?php
      include('connect.php');
  
      $roll=$_REQUEST['roll'];
      $name=$_REQUEST['name'];
      $class=$_REQUEST['class1'];
      $exam_dates=$_REQUEST['exam_date'];
  
      foreach($exam_dates as $e) {
          echo $e;
      }
  
      $a=mysql_query("select * from marks where roll='$roll'");
      if(mysql_num_rows($a)&gt;0) {
          echo 'email already exists';
      } else {
          $insert="insert into marks(roll,name,class,ga,math,english,reasoning,gk)values($roll,$name,$class,$ga,$math,$english,$reasoning,$gk)";
          $result=mysql_query($insert);
  
          if($result) {
              echo ('success');
          } else {
              echo('try again');
          }
      }
  ?&gt;
  </code></pre>
  
  <p>I have first accepted form variables through request superglobals, then some data is fetched through <code>foreach</code>. Then I have written code for inserting data which is not working.
  The error is mainly in "insert" code which is:- "Notice: Undefined variable".</p>