1

I am inserting multiple line values from a html table form in to sql but it's only inserting the last table form value in my database. I can't figure out where the problem is.

Can you help me out with this?

This is my PHP:

$result = mysql_query("SELECT * FROM ex_marks WHERE session='$session' and cl_name='$cl_name' and cl_section='$cl_section' and subject='$subj' and exam='$exam' and date='$date' and roll_no='$rollno' and obtainmarks='$marks'");

if (mysql_num_rows($result) == 0)
{
    mysql_query("INSERT INTO ex_marks(mid, session, cl_name, cl_section, name, fname, status, date, exam, roll_no, subject, obtainmarks, maxmarks, passmarks)
    VALUES('', '$session', '$cl_name', '$cl_section', '$name','$fname', '$attendance', '$date', '$exam', '$rollno', '$subj','$marks','$maxmarks','$passmarks')") or die(mysql_error()); 
    echo "<script type='text/javascript'>alert('Submitted Successfully!')</script>";
} 
else
{ 
    echo "<script type='text/javascript'>alert('Already Exist!')</script>";
}

}

And this is the form where values are inserted:

<div id="page-wrapper">
        <div class="container-fluid">
            <div class="row">
                <div class="col-lg-12">
                    <h1 class="page-header">ASSIGN MARKS</h1>
                    <div class="col-lg-6">
                        <div class="panel">
                                <form method="post">
                            <!--<div class="form-group">
                                <input type="text" size="15" name="date" id="name[]"  class="tcal form-control"  placeholder="EXAM DATE" required="required"/>
                            </div>-->
                            <div class="form-group">
                                <input class="form-control" type="text" size="17" name="maxmarks" placeholder="TOTAL MARKS" required="required"/>
                            </div>
                            <div class="form-group">
                                <input class="form-control" type="text" size="17" name="passmarks" placeholder="PASS MARKS" required="required"/>
                            </div>
                                <button name="btn_sub" type="submit" class="btn btn-info">SUBMIT</button>

                        </div>
                    </div>
                    <!-- /.col-lg-12 -->
                </div>
            </div>
            <!-- /.row -->
            <div class="row">
            <div class="col-lg-12">
                <div class="panel panel-default">
                    <div class="panel-heading">

        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Session :
        <?php
            $session=$_GET['session'];
            echo $session;?>

        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Class :
        <?php
            $cl_name=$_GET['cl_name'];
            echo $cl_name;?>

        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Section :
        <?php
            $cl_section=$_GET['cl_section'];
            echo $cl_section;?>

        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Subject :
        <?php 
            $subj = $_GET['subj'];
            echo $subj;?>

        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Exam Date :
        <?php 
            $date=$_GET['date'];    
            echo $date;?>

        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Exam :
        <?php 
            $exam=$_GET['exam'];
            echo $exam;?>
                    </div>
                    <!-- /.panel-heading -->
                    <div class="panel-body">
                        <div class="dataTable_wrapper">
                            <table class="table table-striped table-bordered table-hover" id="dataTables-example">
                                <thead>
                                    <tr>
                                        <th>NO.</th>
                                        <th class="text-center">NAME</th>
                                        <th class="text-center">ROLL NO</th>
                                        <th class="text-center">FATHER NAME</th>
                                        <th class="text-center">SCORED MARKS</th>
                                        <th class="text-center">ATTENDANCE</th>
                                    </tr>
                                </thead>
                                <tbody>
                        <?php
                            $key="";
                            if(isset($_POST['searchtxt']))
                                $key=$_POST['searchtxt'];
                            if($key !="")
                                $sql_sel1=mysql_query("SElECT * FROM ex_attendance WHERE session like '%$key%' and cl_name like '%$key%' and cl_section like '%$key%'");
                            else
                                $sql_sel1=mysql_query("select * from ex_attendance where session='$session' and cl_name='$cl_name' and cl_section='$cl_section' and exam='$exam' and date='$date' and subject='$subj'");
                            $i=0;
                            while($row1=mysql_fetch_array($sql_sel1))
                            {
                                $i++;
                                $color=($i%2==0)?"lightblue":"white";
                        ?>
                                    <tr class="odd gradeX">
                                        <td><?php echo $i;?></td>
                                        <td align="center"><input size="17" type="text" name="name" value="<?php echo $row1['f_name']." ".$row1['m_name']." ".$row1['l_name'];?>" readonly="readonly"/></td>
                                        <td align="center"><input size="13" type="text" name="rollno" value="<?php echo $row1['roll_no']?>" readonly="readonly"/></td>
                                        <td align="center"><input size="17" type="text" name="fname" value="<?php echo $row1['fname']?>" readonly="readonly"/></td>
                                        <td align="center"><input size="17" type="text" name="marks" required="required"/></td>
                                        <td align="center"><input size="17" type="text" name="attendance" id="name[]" value="<?php echo $row1['status'];?>" readonly="readonly"/></td>
                                    </tr>
                        <?php   }?>
                                </form>
                                </tbody>
                            </table>
                        </div>
                    </div>
                    <!-- /.table-responsive -->
                </div>
                <!-- /.panel-body -->
            </div>
            <!-- /.panel -->
            </div>
            <!-- /.col-lg-12 -->
        </div>
    </div>
Chris G
  • 787
  • 6
  • 20
anup dwivedi
  • 51
  • 11
  • 2
    [Your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Jay Blanchard Jan 27 '16 at 13:19
  • 3
    Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jan 27 '16 at 13:19
  • ok thanks for suggestion sir – anup dwivedi Jan 27 '16 at 13:21
  • And what is your question? – mikeyq6 Jan 27 '16 at 13:26
  • I think your mid is a autoincrement value... if yes then dont give a default empty value. – akhilp2255 Jan 27 '16 at 13:27
  • Take a look on how inserting multiple rows in mysql: http://stackoverflow.com/questions/6889065/inserting-multiple-rows-in-mysql – ADreNaLiNe-DJ Jan 27 '16 at 13:27
  • Possible duplicate of [insert multiple rows via a php array into mysql](http://stackoverflow.com/questions/779986/insert-multiple-rows-via-a-php-array-into-mysql) – Chris G Jan 27 '16 at 13:42
  • not get solution their in my query only last table data inserted in database and all above table data not insert in database thats problem i try for each loop thats also not work :( – anup dwivedi Jan 28 '16 at 05:12

0 Answers0