I’m developing a PHP application that uses the phprets library to synchronize property data from an MLS to a MySQL database. I initially used REPLACE INTO statements, which functioned well because my MLS number was marked as UNIQUE.
However, I encountered a challenge when I integrated geocoding features that necessitated two new columns for latitude and longitude. The REPLACE statement was overwriting the geocoding data I’d manually added, so I transitioned to using INSERT ON DUPLICATE KEY UPDATE instead.
Now, I’m dealing with another problem: the INSERT ON DUPLICATE KEY UPDATE command is not adding new records to the database. While the updates for existing records seem to process correctly, the new entries are not being inserted.
foreach ($property_types as $type) {
echo "Processing: {$type}\n";
$has_next = true;
$offset_value = 1;
$page_limit = 500;
$fields_sequence = array();
while ($has_next) {
$search_parameters = "(PropertyStatus=Active,Pending,Sold),({$modification_timestamp}={$last_timestamp}+)";
echo "Searching: {$search_parameters} Batch Size: {$page_limit} Offset: {$offset_value}\n";
$returned_results = $rets_client->SearchQuery("Property", $type, $search_parameters,
array('Limit' => $page_limit, 'Offset' => $offset_value, 'Format' => 'COMPACT-DECODED', 'Count' => 1));
if ($rets_client->NumRows() > 0) {
if ($offset_value == 1) {
$fields_sequence = $rets_client->SearchGetFields($returned_results);
}
while ($row = $rets_client->FetchRow($returned_results)) {
$record_data = array();
foreach ($fields_sequence as $col) {
$record_data[] = $row[$col];
}
$cleaned_data = str_replace('"', '', $record_data);
$data_string = implode('", "', $cleaned_data);
$finalized_data = '"' . $data_string . '"';
$columns_list = implode(",", $fields_sequence);
$update_fields = array_combine($fields_sequence, $cleaned_data);
foreach ($update_fields as $field => $value) {
$update_fields[$field] = $field . "='" . $value . "'";
}
$update_query = implode(", ", $update_fields);
$query_string = "INSERT INTO MLS_properties ({$columns_list}) VALUES ({$finalized_data}) ON DUPLICATE KEY UPDATE {$update_query}";
mysql_query($query_string);
}
$offset_value += $rets_client->NumRows();
}
$has_next = $rets_client->IsMaxrowsReached();
echo "Total records found: {$rets_client->TotalRecordsFound()}\n";
$rets_client->FreeResult($returned_results);
}
echo "Processing complete\n";
}
What factors could be causing the failure to insert new records while updates proceed normally?