insert multiple table rows via a php into mysql

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • help insert multiple table rows via a php into mysql

    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:


    PHP Code:
    $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>";




    i try to add fro each loop in this but that also not work and add only 1st table data in databse...
    Last edited by Thayla In; 28.01.16, 09:15.

  • #2
    I think more code needs to be seen to help

    Comment


    • #3
      PHP Code:
      <?php
      include('session.php');
      include(
      'connect.php');
          
      $msg="";
          
      $opr="";
          if(isset(
      $_GET['opr']))
          
      $opr=$_GET['opr'];

      if(isset(
      $_GET['rs_id']))
          
      $id=$_GET['rs_id'];

      if(
      $opr=="del")
      {
          
      $del_sql=mysql_query("DELETE FROM stu_tbl WHERE stu_id=$id");
          if(
      $del_sql)
              
      $msg="Record Deleted... !";
          else
              
      $msg="Could not Delete :".mysql_error();            
      }

      if(isset(
      $_POST['btn_sub']))
      {
          foreach(
      $_POST['marks'] as $row=>$Act)
          {    
              
      $session=$_GET['session'];
              
      $cl_name=$_GET['cl_name'];
              
      $cl_section=$_GET['cl_section'];
              
      $exam=$_GET['exam'];
              
      $date=$_GET['date'];
              
      $subj $_GET['subj'];
              
              
      $marks mysql_real_escape_string($Act);
              
      $maxmarks mysql_real_escape_string($_POST['maxmarks'][$row]);
              
      $passmarks mysql_real_escape_string($_POST['passmarks'][$row]);
              
      $name mysql_real_escape_string($_POST['name'][$row]);
              
      $rollno mysql_real_escape_string($_POST['rollno'][$row]);
              
      $fname mysql_real_escape_string($_POST['fname'][$row]);
              
      $attendance mysql_real_escape_string($_POST['attendance'][$row]);
              
              
      $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, maxmarks, passmarks, obtainmarks)
                  VALUES('', '
      $session', '$cl_name', '$cl_section', '$name','$fname', '$attendance', '$date', '$exam', '$rollno', '$subj', '$maxmarks','$passmarks','$marks')") or die(mysql_error());
                  echo 
      "<script type='text/javascript'>alert('Submitted Successfully!')</script>";
              }
              else
              {
                  echo 
      "<script type='text/javascript'>alert('Already Exist!')</script>";
              }
          }
      }
      ?>
      i try for foreach now its only insert one line data in database

      Comment


      • #4
        Try replacing $marks for $Act in your sql

        Comment


        • Ghost
          Ghost commented
          Editing a comment
          looks like marks is act..
          $marks = mysql_real_escape_string($Act);

        • something else
          something else commented
          Editing a comment
          lol well spotted.

          Thayla was saying the last row of form was getting inserted, so if its not a loop problem then I'm guessing its a form problem.
          At a guess I would say that the form data isn't being put into an array, Guessing its overwriting the last row into a string instead.

        • Ghost
          Ghost commented
          Editing a comment
          sounds like you maybe right. in any case. i did correct the awful syntax at least lol.

      • #5
        you should try using the correct syntax when using sql. since words like date is part of sql it needs proper syntax.

        try:
        PHP Code:
        <?php
        include('session.php');
        include(
        'connect.php');
            
        $msg="";
            
        $opr="";
            if(isset(
        $_GET['opr']))
            
        $opr=$_GET['opr'];

        if(isset(
        $_GET['rs_id']))
            
        $id=$_GET['rs_id'];

        if(
        $opr=="del")
        {
            
        $del_sql=mysql_query("DELETE FROM stu_tbl WHERE stu_id=$id");
            if(
        $del_sql)
                
        $msg="Record Deleted... !";
            else
                
        $msg="Could not Delete :".mysql_error();            
        }

        if(isset(
        $_POST['btn_sub']))
        {
            foreach(
        $_POST['marks'] as $row=>$Act)
            {    
                
        $session=$_GET['session'];
                
        $cl_name=$_GET['cl_name'];
                
        $cl_section=$_GET['cl_section'];
                
        $exam=$_GET['exam'];
                
        $date=$_GET['date'];
                
        $subj $_GET['subj'];
                
                
        $marks mysql_real_escape_string($Act);
                
        $maxmarks mysql_real_escape_string($_POST['maxmarks'][$row]);
                
        $passmarks mysql_real_escape_string($_POST['passmarks'][$row]);
                
        $name mysql_real_escape_string($_POST['name'][$row]);
                
        $rollno mysql_real_escape_string($_POST['rollno'][$row]);
                
        $fname mysql_real_escape_string($_POST['fname'][$row]);
                
        $attendance mysql_real_escape_string($_POST['attendance'][$row]);
                
                
        $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`, `maxmarks`, `passmarks`, `obtainmarks`)
                    VALUES('', '
        $session', '$cl_name', '$cl_section', '$name','$fname', '$attendance', '$date', '$exam', '$rollno', '$subj', '$maxmarks','$passmarks','$marks')") or die(mysql_error());
                    echo 
        "<script type='text/javascript'>alert('Submitted Successfully!')</script>";
                }
                else
                {
                    echo 
        "<script type='text/javascript'>alert('Already Exist!')</script>";
                }
            }
        }
        ?>

        we may need to see your file with form details to be able to diagnose the problem further.
        but you should still fix the sql syntax like i mentioned above.
        Last edited by Ghost; 29.01.16, 14:19.

        Comment


        • #6
          chk this file with form too when i use ur code with stop 3 alert


          PHP Code:
          include('session.php');
          include(
          'connect.php');
              
          $msg="";
              
          $opr="";
              if(isset(
          $_GET['opr']))
              
          $opr=$_GET['opr'];

          if(isset(
          $_GET['rs_id']))
              
          $id=$_GET['rs_id'];

          if(
          $opr=="del")
          {
              
          $del_sql=mysql_query("DELETE FROM stu_tbl WHERE stu_id=$id");
              if(
          $del_sql)
                  
          $msg="Record Deleted... !";
              else
                  
          $msg="Could not Delete :".mysql_error();            
          }

          if(isset(
          $_POST['btn_sub']))
              {
                  
          $flg=0;
                  foreach(
          $_POST['marks'] as $row=>$Act)
                  {    
                      
          $session=$_GET['session'];
                      
          $cl_name=$_GET['cl_name'];
                      
          $cl_section=$_GET['cl_section'];
                      
          $exam=$_GET['exam'];
                      
          $date=$_GET['date'];
                      
          $subj $_GET['subj'];

                  
          $marks mysql_real_escape_string($Act);
                  
          $maxmarks mysql_real_escape_string($_POST['maxmarks'][$row]);
                  
          $passmarks mysql_real_escape_string($_POST['passmarks'][$row]);
                  
          $name mysql_real_escape_string($_POST['name'][$row]);
                  
          $rollno mysql_real_escape_string($_POST['rollno'][$row]);
                  
          $fname mysql_real_escape_string($_POST['fname'][$row]);
                  
          $attendance mysql_real_escape_string($_POST['attendance'][$row]);

                  
          $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`, `maxmarks`, `passmarks`, `obtainmarks`)
                      VALUES('', '
          $session', '$cl_name', '$cl_section', '$name','$fname', '$attendance', '$date', '$exam', '$rollno', '$subj', '$maxmarks','$passmarks','$marks')") or die(mysql_error());
                      
          $flg=1;
                  }
                  else
                  {
                      
          $flg=0;
                      exit;
                  }
              }
              if(
          $flg==1)
              {
                   echo 
          "<script type='text/javascript'>alert('Submitted Successfully!')</script>";    
              }
              else
              {
                  echo 
          "<script type='text/javascript'>alert('Already Exist!')</script>";
              }
              } 

          but again now only obtain mark save in all table but pass marks and total marks save in 1st table only
          Attached Files
          Last edited by Thayla In; 30.01.16, 07:29.

          Comment


          • something else
            something else commented
            Editing a comment
            Download isn't working for me >.<

        • #7
          something else i havent had time to look through it yet i currently have to be somewhere.
          but heres the download file. the download worked for me.

          PHP Code:
          <?php
          include('session.php');
          include(
          'connect.php');
              
          $msg="";
              
          $opr="";
              if(isset(
          $_GET['opr']))
              
          $opr=$_GET['opr'];

          if(isset(
          $_GET['rs_id']))
              
          $id=$_GET['rs_id'];

          if(
          $opr=="del")
          {
              
          $del_sql=mysql_query("DELETE FROM stu_tbl WHERE stu_id=$id");
              if(
          $del_sql)
                  
          $msg="Record Deleted... !";
              else
                  
          $msg="Could not Delete :".mysql_error();            
          }

          if(isset(
          $_POST['btn_sub']))
          {
              foreach(
          $_POST['marks'] as $row=>$Act)
              {    
                  
          $session=$_GET['session'];
                  
          $cl_name=$_GET['cl_name'];
                  
          $cl_section=$_GET['cl_section'];
                  
          $exam=$_GET['exam'];
                  
          $date=$_GET['date'];
                  
          $subj $_GET['subj'];
                  
                  
          $marks mysql_real_escape_string($Act);
                  
          $maxmarks mysql_real_escape_string($_POST['maxmarks'][$row]);
                  
          $passmarks mysql_real_escape_string($_POST['passmarks'][$row]);
                  
          $name mysql_real_escape_string($_POST['name'][$row]);
                  
          $rollno mysql_real_escape_string($_POST['rollno'][$row]);
                  
          $fname mysql_real_escape_string($_POST['fname'][$row]);
                  
          $attendance mysql_real_escape_string($_POST['attendance'][$row]);
                  
                  
          $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`, `maxmarks`, `passmarks`, `obtainmarks`)
                      VALUES('', '
          $session', '$cl_name', '$cl_section', '$name','$fname', '$attendance', '$date', '$exam', '$rollno', '$subj', '$maxmarks','$passmarks','$marks')") or die(mysql_error());
                      echo 
          "<script type='text/javascript'>alert('Submitted Successfully!')</script>";
                  }
                  else
                  {
                      echo 
          "<script type='text/javascript'>alert('Already Exist!')</script>";
                  }
              }
          }
          ?>

                  <!-- Page Content -->
                  <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">
                                              
                                          <!--<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="input-group custom-search-form">
                                              <form method="post">
                                          </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>
          if i can ill take a look later also. until then. ill post his download here.

          Comment


          • #8
            The form is all good. (apart from the missing form opening)
            I think by the looks of things that:
            $maxmarks = mysql_real_escape_string($_POST['maxmarks'][$row]);
            $passmarks = mysql_real_escape_string($_POST['passmarks'][$row]);

            should be:

            $maxmarks = mysql_real_escape_string($_POST['maxmarks']);
            $passmarks = mysql_real_escape_string($_POST['passmarks']);

            As otherwise they would return blank - making the mysql query's wrong

            Comment


            • something else
              something else commented
              Editing a comment
              maybe wrong as form has maxmarks[]

            • Ghost
              Ghost commented
              Editing a comment
              the beggining of form is there. its just small is all.
              PHP Code:
              <form method=&quot;post&quot;> 
              marks[] is also there:
              PHP Code:
              <td align=&quot;center&quot;><input size=&quot;17&quottype=&quot;text&quotname=&quot;marks[]&quotrequired=&quot;required&quot;/></td
              but this is confusing me. it all looks like it should go together.
              but some reason it sounds like
              PHP Code:
              foreach($_POST['marks'] as $row=>$Act
              is only bringing 1 result up not multiple.

              without actually running the script myself and seeing the error/results in progress
              im not sure its gonna get resolved.

              ps: not sure why codebox's are showing **** loads of &quot; (shame)
              Last edited by Ghost; 31.01.16, 22:25.

            • something else
              something else commented
              Editing a comment
              yeah iv had similar problems with the [ php] bbcode - [ code] bbcode seems to work ok though

          • #9
            have you tried using:
            PHP Code:
            print_r($_POST['marks']); 
            to see what marks is actually producing?

            Comment


            • something else
              something else commented
              Editing a comment
              Just tested
              print_r($_POST['marks']); returns fine

              It is what i said maxmarks and passmarks are empty

              its because $row is marks:
              foreach($_POST['marks'] as $row=>$Act)

              so:
              $_POST['maxmarks'][$row];
              does not make seance as it is nothing to do with marks and ideally shouldn't even be in the loop
              (its a separate input above the table)
              Last edited by something else; 01.02.16, 01:23.

          • #10
            So recap to Thayla:

            change:
            $maxmarks = mysql_real_escape_string($_POST['maxmarks'][$row]);
            $passmarks = mysql_real_escape_string($_POST['passmarks'][$row]);

            to:

            $maxmarks = mysql_real_escape_string($_POST['maxmarks']);
            $passmarks = mysql_real_escape_string($_POST['passmarks']);

            and remove them out of the foreach loop

            and also change:

            <input class="form-control" type="text" size="17" name="maxmarks[]" placeholder="TOTAL MARKS" required="required"/>
            <input class="form-control" type="text" size="17" name="passmarks[]" placeholder="PASS MARKS" required="required"/>

            to:

            <input class="form-control" type="text" size="17" name="maxmarks" placeholder="TOTAL MARKS" required="required"/>
            <input class="form-control" type="text" size="17" name="passmarks" placeholder="PASS MARKS" required="required"/>

            unless you plan on putting them on a different foreach loop

            Comment


            • #11
              gr888 thanks lot guys thank you so much

              Comment

              Working...
              X