I am having some difficulty with MySQL seeming to insert a row twice. - Basically, I need to save a local copy of some information that is retrieved from a remote data source. So when a user views information that is from the remote source, I check to see if I have a local copy of the information I need to store. If I don't have a local copy I add a record of that information. - The issue I am having is that every I would say 20-30 inserts I get a duplicate. I keep track of the insert and update times using the NOW function, and both records seem to be inserted at the same time. Here is my PHP code, any help would be very appreciated, I am stumped:
// We have the location, see if we have a local record for that location
$idLocation = locationID_for_factualID($factual_id);
if(!$idLocation) {
// We do not have local information about the location, add it
$mysqli = open_mysql_connection();
$stmt = $mysqli->prepare("INSERT INTO Location (
factual_id,
dateAdded,
dateModified,
locationName,
latitude,
longitude)
VALUES (?, NOW(), NOW(), ?, ?, ?)");
if($stmt) {
$stmt->bind_param("ssdd",$factual_id, $location["name"], doubleval($location["latitude"]), doubleval($location["longitude"]));
$stmt->execute();
// Check if the location was added
if($stmt->affected_rows == 1){
$idLocation = locationID_for_factualID($factual_id);
}
$stmt->close();
$mysqli->close();
}
else {
return FALSE;
}
}
Here are two rows that seem to be inserted back to back:
idLocation | factual_id | dateAdded | dateModified | locationName | latitude | longitude
520 | 5f79360f-330f-4035-ae75-e872ea14cfdd | 2013-04-09 14:36:55 | 2013-04-09 14:36:55 | Quiznos | 40.1802 | -74.0258
521 | 5f79360f-330f-4035-ae75-e872ea14cfdd | 2013-04-09 14:36:55 | 2013-04-09 14:36:55 | Quiznos | 40.1802 | -74.0258