需要将数据从动态创建的表单行插入到不同的mysql表中

时间:2018-08-10 11:57:20

标签: javascript mysql node.js express ejs

具有动态列表的表格,该表格需要将数据发布到每个员工的各自表中:

enter image description here

前端代码HTML

<tbody>
<% aitems.forEach(function(item){ %>
<tr>

  <td>
    <div>
      <%= item.fullname %>
    </div>
    <div class="small text-muted">
      <%=  item.designation %>
    </div>
  </td>
  <td>
    <div>
      <input type="number" step=0.1 class="form-control" data-id="<%=  item.idemployee %>" onkeyup="attendance_calculation(this)"
             id="sick_leave<%=  item.idemployee %>" name="sick_leave<%=  item.idemployee %>" placeholder="Sick Leave">
      <% if(item.available_sick_leave==0){ %>
      <div class="leave-alert">No Sick Leave Available. So will cost in LOP</div>
      <% }%>
      <input type="number" step=0.1 class="form-control" data-id="<%=  item.idemployee %>" id="available_sick_leave<%=  item.idemployee %>"
             name="available_sick_leave<%=  item.idemployee %>" value="<%=  item.available_sick_leave %>"
             placeholder="Sick Leave" hidden>
    </div>
  </td>
  <td>
    <div>
      <input type="number" step=0.1 class="form-control" data-id="<%=  item.idemployee %>" onkeyup="attendance_calculation(this)"
             id="casual_leave<%=  item.idemployee %>" name="casual_leave<%=  item.idemployee %>" placeholder="casual Leave">
      <% if(item.available_casual_leave==0){ %>
      <div class="leave-alert">No Casual Leave Available. So will cost in LOP</div>
      <% }%>
      <input type="number" step=0.1 class="form-control" data-id="<%=  item.idemployee %>" id="available_casual_leave<%=  item.idemployee %>"
             name="available_casual_leave<%=  item.idemployee %>" value="<%=  item.available_casual_leave %>"
             placeholder="Sick Leave" hidden>
    </div>
  </td>
  <td>
    <div>
      <input type="number" step=0.1 class="form-control" data-id="<%=  item.idemployee %>" id="total_work_days<%=  item.idemployee %>"
             name="total_work_days<%=  item.idemployee %>" value="30" placeholder="Present Days">
    </div>
  </td>
  <td>
    <div>
      <input type="number" step=0.1 class="form-control" name="overtime<%=  item.idemployee %>" placeholder="Over Time Hours">
    </div>
  </td>


</tr>

<% }); %>

</tbody>

前端代码JS

if (document.getElementById(available_sick_leave).value != 0) {
            if (document.getElementById(sick_leave).value != 0) {
                    var sick_leave = (document.getElementById(sick_leave).value) - 1 || 0;
                    // var deduct_sick_leave = (document.getElementById(available_sick_leave).value) - 1;
                    // document.getElementById(available_sick_leave).value = deduct_sick_leave;
            }
            else {
                    var sick_leave = (document.getElementById(sick_leave).value) || 0;
            }
    }
    else {
            if (document.getElementById(sick_leave).value != 0) {
                    var sick_leave = (document.getElementById(sick_leave).value) || 0;
            }
            else {
                    var sick_leave = (document.getElementById(sick_leave).value) || 0;
            }
    }
    if (document.getElementById(available_casual_leave).value != 0) {
            if (document.getElementById(casual_leave).value != 0) {
                    var casual_leave = (document.getElementById(casual_leave).value) - 0.5 || 0;
                    // var deduct_casual_leave = (document.getElementById(available_casual_leave).value) - 0.5;
                    // document.getElementById(available_casual_leave).value = deduct_casual_leave;
            }
            else {
                    var casual_leave = (document.getElementById(casual_leave).value) || 0;
            }
    } else {
            if (document.getElementById(casual_leave).value != 0) {
                    var casual_leave = (document.getElementById(casual_leave).value) || 0;
            }
            else {
                    var casual_leave = (document.getElementById(casual_leave).value) || 0;
            }

    }

后端节点JS代码

exports.add = function (req, res) {
var employee_list = "SELECT * FROM `payroll`.`employee-list` WHERE status = 'Active' ORDER BY fullname ASC;"
db.query(employee_list, function cal1(err, result1) {
    if (err) throw err;
    else {
        var query = result1.forEach(function cal(item) {
            var sl_name = "sick_leave" + item.idemployee;
            var cl_name = "casual_leave" + item.idemployee;
            var tp_name = "total_work_days" + item.idemployee;
            var ot_name = "overtime" + item.idemployee;
           console.log("INSERT INTO `payroll`.`" + item.username + "` (`month`, `year`, `sick_leave`, `casual_leave`, `total_work_days`, `overtime`) VALUES (monthname(now()- INTERVAL 1 MONTH), year(now()- INTERVAL 1 MONTH), '" + req.body.sl_name + "', '" + req.body.cl_name + "', '" + req.body.tp_name + "', '" + req.body.ot_name + "');");
            });
            db.query(query, function (err, result2) {
                if(err) throw err;
                else{
                    res.redirect("http://payroll.luftek.in/employee-list")
                }
        });
    }

})

}

我的要求是将数据发布到每个员工的mysql服务器表中。 sql查询代码应在雇员列表上动态生成的位置。当它不为零时,还必须进一步从每个雇员的主表的列中扣除价值。

请帮助我解决它。

0 个答案:

没有答案