2015-10-20 65 views
2

我正在使用工资系统。管理员能够成功保存这些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

+0

你想要它被添加到数据库?或者你打算避免这种情况? – Sina

+0

@Sina,我相信她想加入数据库,因为他想要“薪水历史”。有没有机会让这个专栏成为“唯一索引”? – Mustafa

+0

Hi Sina!是的,它应该被添加到数据库中。 –

回答

0

我认为 '.... similiar员工......' 由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 
1

看看你的表结构,Id必须是PRIMARY KEY这就是为什么当你选择一个现有的ID时不能被添加的原因。
由于主键不能重复,所以如果你想拥有这个过程,结构必须改变。