0

Something very bizare is happening right now and I don't understand how to fix it... Well, temporarily I do but the solution I found is not how the program should behave...

So I have this employeetimesheets table:

empsheet_id | employee_id |timesheet_status |last_update |isWeekNumber|total_hours|isMonth|isYear|overtimeHours

the variables:

    $isWeekNumber =  $_POST['isWeekNumber'];
    $attendanceyear= $_POST['attendanceyear'];
    $attendancemonth= $_POST['attendancemonth'];
    $employee_id=  $_POST['employee_id'];

the query:

    $insertemptime= $db->prepare("INSERT INTO employeetimesheets (employee_id,isWeekNumber,isMonth,isYear) VALUES (?,?,?,?)");
    $insertemptime->bind_param("iiss",$employee_id,$isWeekNumber,$attendancemonth,$attendanceyear);
    $insertemptime->execute() or die(mysqli_error($db)); 
    $insertemptime->close();

On insert NEW row I receive this error when there is already a row with same employee_id

Fatal error: Uncaught exception 'mysqli_sql_exception' with message 'Duplicate entry '6748372' for key 'EmployeeTimeSheetsKey''

If I delete the row with the employee_id I'm trying to insert, the query works. I"m confused because it's an insert statement... I should be able to add as many rows I want with the same employee_id, yes? Anybody has an idea what's going on?

3
  • Fatal error: Uncaught exception 'mysqli_sql_exception' with message 'Duplicate entry '6748372' for key 'EmployeeTimeSheetsKey'' You are getting this message because you have already an entry with6748372 id in your mysql table Commented Apr 1, 2017 at 2:09
  • @SahilGulati: I know this is why i'm confused. Commented Apr 1, 2017 at 2:10
  • For that you have to do two things i am updating my post for that Commented Apr 1, 2017 at 2:11

1 Answer 1

3

You've set the field employee_id to unique instead of index

Remove unique index from field and add index again without unique

Example sql code

  1. Check index

    SHOW INDEX FROM employeetimesheets;
    
  2. Remove Index

    ALTER TABLE employeetimesheets DROP INDEX employee_id;
    
  3. Add Index

    CREATE INDEX employeetimesheets ON employee_id;
    
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.