0

I have to insert data into databases through foreach, but it is not working...

snapshot of form $a=mysql_query("select * from subject where batch='$cl'"); if(mysql_num_rows($a)>0) { $result=mysql_fetch_array($a) ?>

$exp_array=explode(',',$result[3]);
foreach($exp_array as $vl) {
?>
    <div class="row">
        <div class="col-sm-6 pad-left">  
            <?php echo '<br/>'; echo $vl; ?>
        </div> 
        <div class="col-sm-6 pad-left"> 
            <input  name="exam_date[]" type="text" placeholder="<?php echo $vl;?>" class="form-control">
        </div>
    </div>
<?php } ?>
<br/>
<button type="submit" class="btn btn-success col-sm-offset-4">Submit</button>

</div>
</div>

This code is to insert data dynamically from database which is stored from another table.

<?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)>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');
        }
    }
?>

I have first accepted form variables through request superglobals, then some data is fetched through foreach. Then I have written code for inserting data which is not working. The error is mainly in "insert" code which is:- "Notice: Undefined variable".

LoïcR
  • 4,940
  • 1
  • 34
  • 50
  • Yes undefined: `$ga,$math,$english,$reasoning,$gk`. Where did you define? – Thamilhan May 10 '17 at 06:29
  • `mysql_*` is deprecated - please do not use. Instead use `mysqli` or `PDO` - both of which allow you to use `prepared statements` which make inserting records quicker and safer – Professor Abronsius May 10 '17 at 06:30

0 Answers0