Code Kris
Code Kris

Reputation: 447

how to avoid the multiple execution of insert query

I am working with OT system.i have some problem regrading OT hours request program.this program send the department employee OT hours in to the admin panel notification and OT hours save in the database.my problem is when send OT request each person create the separate OT notification .I want send the single notification set of the department people.

$(document).on('click', '.overtime_send', function() {
   temp = 0;
  $('#employee_table tbody tr').each(function(row, tr) {

    var emp_no = $(tr).find('td:eq(0)').text();
    var ot_hours = $(tr).find('input').val();

    //ot_array.push([emp_no,ot_hours]);

    $.ajax({
      url: 'otrequset_action.php',
      type: 'POST',
      data: { action:'add_ot',emp_no : emp_no, ot_hours:ot_hours},
      dataType:"json",
      success:function(data)
      {
        if(data.success)
        {
         swal("Good job!", "OverTime Request Send Successfully!", "success");
         temp = 1;
         dataTable.ajax.reload();  
        }
      }
    });  
  });
  alert(temp);//if data.success block execute but alert still get value as 0  
  if (temp == 1) {
$.ajax({
      url: 'otrequset_action.php',
      type: 'POST',
      data: { action:'add_comment'},
      dataType:"json",
      success:function(data)
      {
        if(data.success)
        {  
         dataTable.ajax.reload();
        }
      }
    });
}  

i all ready got some solution .i get create the temp variable and OT request process successful it assign temp = 1 and after the each loop i try to insert notification data into the database.but if data.success success but alert(temp); not get value as 1 .how do i solve this question.

back end code

if($_POST["action"] == 'add_ot')
      { 
        $today = date("Y-m-d");
        $department = $_SESSION["dept_Id"];
        $state = 5 ;
        $emp_no = $_POST["emp_no"];
        $ot_hours = $_POST["ot_hours"];

        if($ot_hours != '00:00'){

        $data = array(
          ':today'                =>  $today,
          ':department'           =>  $department,
          ':emp_no'               =>  $emp_no,
          ':ot_hours'             =>  $ot_hours,
          ':state'                =>  $state

        );
        $query = "
        INSERT INTO otresquest 
        (emp_id,date,ot_hour,dept_Id,overtime_status) 
        VALUES (:emp_no, :today, :ot_hours,:department,:state)
        ";
        $statement = $connect->prepare($query);
        if($statement->execute($data))
        {
          $output = array(
            'success'   =>  'OT request has been Sent succesfully',
          );
        }
      }
        echo json_encode($output);
      }

  if($_POST["action"] == 'add_comment')
      { 
        $today = date("Y-m-d");
        $department = $_SESSION["dept_Id"];
        $comment_status = 0 ;

        $data = array(
          ':today'                =>  $today,
          ':department'           =>  $department,
          ':state'                =>  $comment_status

        );
        $query = "
        INSERT INTO comments 
        (dept_Id,date,comment_status) 
        VALUES (:department,:today,:state)
        ";
        $statement = $connect->prepare($query);
        $statement->execute($data);
    }

Upvotes: 0

Views: 64

Answers (1)

Nipun Sachinda
Nipun Sachinda

Reputation: 430

**This might be solve your problem**

(document).on('click', '.overtime_send', function() {
   temp = 0;
  $('#employee_table tbody tr').each(function(row, tr) {

    var emp_no = $(tr).find('td:eq(0)').text();
    var ot_hours = $(tr).find('input').val();

    //ot_array.push([emp_no,ot_hours]);

    $.ajax({
      url: 'otrequset_action.php',
      type: 'POST',
      data: { action:'add_ot',emp_no : emp_no, ot_hours:ot_hours},
      dataType:"json",
      success:function(data)
      {
         swal("Good job!", "OverTime Request Send Successfully!", "success");
         temp++;
         dataTable.ajax.reload();
           if (temp == 1) {
           $.ajax({
              url: 'otrequset_action.php',
              type: 'POST',
              data: { action:'add_comment'},
              dataType:"json",
              success:function(data)
              {
                if(data.success)
                {  
                 dataTable.ajax.reload();
              }
             }
          });
        }  
    }
   });  
});

   swal({title: "No data entering?",text: "Not set the overtime hours !",type: "warning"});

});

Upvotes: 1

Related Questions