Im inserting the daily attendance. I want to avoid duplicate entries. The unique columns should be the month, day and ID altogether. My condition is "if month and day exist for that employee ID,(For example, if there's already an attendance for March 22 for employee#40) it should return an error and should not proceed to inserting. This is my code.
$avoid_double_entry = query("SELECT * FROM attendance WHERE
employee_id = '$employee_id' AND month = '$month' AND day = '$day' ");
confirm($avoid_double_entry);
if($row = fetch_array($avoid_double_entry)) {
echo "already exists";
} else {
echo "no records found";
<--proceed to insert query-->
However, when I run this code, and when there's an entry for DAY 22, even the month is not March and the employee ID is not 40, it says already exists. It also happens when the day is not 22 but the month is March, vice versa.
It only says no records found when the three conditions are not met. I kind of understand this part. What I don't understand is why it says already exists even only one condition is met. Thanks in advance for your help.
I already tried not query WHERE NOT, DUPLICATE KEY UPDATE, but still cant figure it out..
By the way, this is my confirm function which is working fine, but just in case.
function confirm($result) {
global $connection;
if(!$result) {
die("Query failed" . mysqli_error($connection));
}
}