Home > Mobile >  Mysql Duplicates
Mysql Duplicates

Time:09-17

I would like to avoid duplicate entries in DB.

  1. In php I check if record exists, before insert takes place.
$lead = $CI->db->get_where(db_prefix(). 'leads', ['fbid' => $sender]);
if ($lead->num_rows() == 0) {
    $CI->social_model->create($sender);
}
  1. In mysql i have UNIQUE parameter set to fbid.

Issue is that sometimes facebook sends identical queries at the same time, and my php check fails because of that reason. I was wondering if there is any way to avoid duplicates on mysql end for the queries that are sent at the same time. (or any other way is welcome)

CodePudding user response:

Your problem is, that you have a Unique key over the fbid and the id column. So when you insert a new Row into the table you have a unique combination of id and fbid, because when you insert the same data again, you get an updated id value, so the constraint is met.

Your constraint should only contain the fbid

Or, when you want more than one entry for an fbid you should look if you have any other value in the response from api that can be used for the constraint (like a timestamp )

  • Related