【问题标题】:Not inserting values to database when data is similar数据相似时不向数据库插入值
【发布时间】:2015-11-02 07:35:34
【问题描述】:

我正在开发一个工资系统。 The admin is able to successfully save these data but when an employee ID, which is already in the database, is selected, it is not added to the database.这是我的代码:

$sql = "INSERT INTO payroll VALUES('$id','$period','$paidDays','$hourlyRate','$hoursworked','$overtime','$overtimePay','$undertime','undertimePay','$sss','$philHealth','$pagibig','$OtherDeductions','$tax','$grossPay','$netPay')";

我需要解决这个问题才能记录员工的薪资历史。

请注意此代码有效。只是当输入了类似的员工时,并没有添加到数据库中。谢谢!

已编辑:这是table structure

【问题讨论】:

  • 您希望将其添加到数据库中吗?还是您打算避免这种情况?
  • @Sina,我相信她想添加到数据库,因为他想拥有“工资历史”。您是否有机会将该列设为“唯一索引”?
  • 你好新浪!是的,它应该被添加到数据库中。
  • 嗨 Mustafa,我没有让它独一无二
  • 你能把你的表结构贴出来

标签: php mysql sql


【解决方案1】:

看你的表结构,id一定是PRIMARY KEY,所以选择现有id时不能添加。
由于主键不能重复,所以如果你想拥有那个过程,就必须改变结构。

【讨论】:

    【解决方案2】:

    我假设 '..similiar employee...' 是由 ID 定义的,这可能会给您一些提示:

     Insert Into Payroll
     Select id,period,paidDays,hourlyRate,hoursworked, overtime,overtimePay,
     undertime,undertimePay,sss,philHealth,pagibig,OtherDeductions,
     tax,grossPay,netPay
     FROM (
     select '$id' AS ID,'$period' AS period,'$paidDays' AS  paidDays,
     '$hourlyRate' AS hourlyRate, '$hoursworked' AS hoursworked,
     '$overtime' AS overtime,'$overtimePay' AS overtimePay,
     '$undertime' AS undertime,'undertimePay' AS undertimePay,
     '$sss' AS sss,'$philHealth' AS philHealth,'$pagibig' AS pagibig,
     '$OtherDeductions' AS OtherDeductions,'$tax' AS tax,
     '$grossPay' AS grossPay,'$netPay' AS netPay, 0 AS JUM
      UNION ALL
      select id,period,paidDays,hourlyRate,hoursworked,  overtime,
      overtimePay, undertime,undertimePay,sss,philHealth,pagibig,
      OtherDeductions,tax,grossPay,netPay, 1 AS JUM
      from [YourTable]
      Where [ID] = [EmployeeID]) AS T
      GROUP BY id,period,paidDays,hourlyRate,hoursworked, overtime,
      overtimePay, undertime,undertimePay,sss,philHealth,pagibig,
      OtherDeductions,tax,grossPay,netPay
      HAVING SUM(JUM) <0
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多