Welcome Guest, Not a member yet? Register   Sign In
duplicate entry error. How to catch?
#1

(This post was last modified: 07-06-2021, 10:53 AM by richb201.)

I have a table where I am inserting rows. I have created the table with this:
$sql = "CREATE TABLE BC_PR_list_temp(
          taxyear  VARCHAR(10),
          employee_email VARCHAR(80),
          employee  VARCHAR(80),       
          qualified_dollars int NOT NULL,
          title varchar(80),
          role varchar(80),
          num_BC int NOT NULL,
          num_PR int NOT NULL,
          type varchar(4),
          item varchar(80),
          dollarsPerBC int NOT NULL,
          dollarsPerPR int NOT NULL,
          business_component_string varchar(120),
          project_string varchar(120),
          CONSTRAINT OnlyOne UNIQUE (employee_email, item, qualified_dollars)
                    )";
$this->db->query($sql);
At some later time I am inserting rows but sometimes insert the same row twice. That is why I put in the Constraint. Now when I try to put in the same row twice, I get this error:
Error Number: 1062
Duplicate entry '[email protected]' for key 'BC_PR_list_temp.OnlyOne' 
That is a correct call to constraint but I want to catch that error and just continue. How do I just ignore that error. Right now the entire program stops!
proof that an old dog can learn new tricks
Reply
#2

@richb201 ,

Try insert ignore (https://dev.mysql.com/doc/refman/5.7/en/insert.html). I'm also assuming that you have a more recent or current version of MySQL.
Reply
#3

(07-06-2021, 12:55 PM)php_rocs Wrote: @richb201 ,

Try insert ignore (https://dev.mysql.com/doc/refman/5.7/en/insert.html). I'm also assuming that you have a more recent or current version of MySQL.

Yeah I am using a version hosted on AWS so it is likely pretty current. 
proof that an old dog can learn new tricks
Reply
#4

(This post was last modified: 07-06-2021, 02:37 PM by richb201.)

I think I will let the code fill in all the duplicates and then when it is done remove the duplicates with
DELETE t1 FROM contacts t1
INNER JOIN contacts t2
WHERE
t1.id < t2.id AND
t1.email = t2.email;

But this requires an autoincrment id.
I can use "id int NOT NULL PRIMARY KEY AUTO_INCREMENT" in my Create table.
But when I try to insert I get a duplicate key error. I have tried
INSERT INTO BC_PR_list_temp SELECT NULL,srat.taxyear , srat.employee_email , etc
But I get this:
Error Number: 1062

Duplicate entry '1' for key 'BC_PR_list_temp.PRIMARY'

Any ideas how to get the insert to work? I need the id in order to use the code above.
proof that an old dog can learn new tricks
Reply
#5

(This post was last modified: 07-06-2021, 06:28 PM by richb201.)

SOLVED! I needed to use the keyword IGNORE.
"INSERT IGNORE INTO BC_PR_list_temp SELECT srat.taxyear , srat.employee_email

Thank you!
proof that an old dog can learn new tricks
Reply
#6

bad table design, u need primary key (with auto_increment) to get it work well in future.
Reply
#7

(This post was last modified: 07-07-2021, 03:34 AM by richb201.)

(07-07-2021, 12:03 AM)ikesela Wrote: bad table design, u need primary key (with auto_increment) to get it work well in future.

This is a temporary table. It gets used once to run koolreport on, for rendering. But please tell me how I insert a select into a autoincrement field for the future?
proof that an old dog can learn new tricks
Reply




Theme © iAndrew 2016 - Forum software by © MyBB