2015-10-20 66 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時不能被添加的原因。
由於主鍵不能重複,所以如果你想擁有這個過程,結構必須改變。