数据相似时不向数据库插入值

时间:2015-10-20 04:41:40

标签: php mysql sql

我正在研究薪资系统。管理员能够成功保存这些data,但是当选择了已存在于数据库中的员工ID时,它不会添加到数据库中。这是我的代码:

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

我需要解决这个问题,以便能够完成员工的薪资历史。

请注意,此代码正常运行。只是当输入类似的员工时,它不会被添加到数据库中。谢谢!

已编辑:这是table structure

2 个答案:

答案 0 :(得分:1)

查看您的表格结构,ID必须为 PRIMARY KEY ,这就是为什么在您选择现有ID时无法添加的原因。
由于主键不能重复,因此如果您想要进行该过程,则必须更改结构。

答案 1 :(得分:0)

我认为'....类似员工......'是由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