added feature to update inserted information, fixed bug that was inserting new rows after each refresh of the page and optimized get_customfield_values , it was sending many requests and optimized to 1.

This commit is contained in:
2025-08-21 20:39:31 +04:00
parent 2c514a8ab6
commit 24cda34681
5 changed files with 363 additions and 112 deletions
+51 -15
View File
@@ -14,28 +14,64 @@ try {
$db = DBHandlerSelect::getInstance();
$pdo = $db->getConnection();
// Prepara i dati da aggiornare
$updates = [];
$values = [];
foreach ($_POST as $key => $value) {
if ($key !== 'iddatadb') {
$updates[] = "$key = ?";
$values[] = htmlspecialchars($value);
$data = $_POST;
$details = [];
// 1. POST-დან ამოვიღოთ მხოლოდ details
foreach ($data as $key => $value) {
if (preg_match('/^details(\d+)field_value$/', $key, $matches)) {
$id = $matches[1];
$details[$id] = $value;
}
}
$values[] = $iddatadb;
if (empty($updates)) {
throw new Exception('Nessun dato da aggiornare');
// 2. DB-დან წამოვიღოთ არსებული მნიშვნელობები
$stmt = $pdo->prepare("SELECT mapping_id, field_value FROM import_data_details WHERE id = ?");
$stmt->execute([$iddatadb]);
$currentValues = [];
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$currentValues[$row['mapping_id']] = $row['field_value'];
}
$sql = "UPDATE datadb SET " . implode(', ', $updates) . " WHERE iddatadb = ?";
$stmt = $pdo->prepare($sql);
$stmt->execute($values);
// 3. შევადაროთ POST-ს და DB-ს
$changed = [];
foreach ($details as $id => $newValue) {
$oldValue = $currentValues[$id] ?? null;
if ($oldValue !== $newValue) {
$changed[$id] = [
'old' => $oldValue,
'new' => $newValue
];
}
}
// 4. თუ არის ცვლილებები → UPDATE
if (!empty($changed)) {
$updateStmt = $pdo->prepare("
UPDATE import_data_details
SET field_value = :newValue
WHERE id = :iddatadb AND mapping_id = :mappingId
");
foreach ($changed as $mappingId => $values) {
$updateStmt->execute([
':newValue' => $values['new'],
':iddatadb' => $iddatadb,
':mappingId' => $mappingId
]);
}
$response['success'] = true;
$response['message'] = "Updated successfully";
$response['changed'] = $changed; // Debug / optional
} else {
$response['success'] = true;
$response['message'] = "No changes found";
}
$response['success'] = true;
$response['message'] = 'Riga aggiornata con successo';
} catch (Exception $e) {
$response['success'] = false;
$response['message'] = $e->getMessage();
error_log("Errore in save_edited_row.php: " . $e->getMessage());
}