up
This commit is contained in:
+32
-2
@@ -47,7 +47,9 @@ class Database {
|
||||
}
|
||||
return $stmt;
|
||||
} catch (PDOException $e) {
|
||||
$errorDetail = date('[Y-m-d H:i:s] ') . "DB Query FAILED: " . $e->getMessage() . "\n SQL: " . $sql . "\n Params: " . json_encode($params) . "\n Trace: " . $e->getTraceAsString() . "\n\n";
|
||||
error_log("Query failed: " . $e->getMessage() . " SQL: " . $sql . " Params: " . json_encode($params));
|
||||
@file_put_contents(__DIR__ . '/../logs/app_errors.log', $errorDetail, FILE_APPEND);
|
||||
throw new Exception("Error en la consulta a la base de datos: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
@@ -90,8 +92,36 @@ class Database {
|
||||
}
|
||||
$fieldsStr = implode(', ', $fields);
|
||||
|
||||
$sql = "UPDATE {$table} SET {$fieldsStr} WHERE {$where}";
|
||||
$params = array_merge($data, $whereParams);
|
||||
// Convertir WHERE params para evitar mezcla de nombrados y posicionales
|
||||
$whereNamed = $where;
|
||||
$namedWhereParams = [];
|
||||
|
||||
if (!empty($whereParams)) {
|
||||
// Detectar si es asociativo (nombrado) o secuencial (posicional)
|
||||
$keys = array_keys($whereParams);
|
||||
$isAssociative = ($keys !== range(0, count($whereParams) - 1));
|
||||
|
||||
if ($isAssociative) {
|
||||
// WHERE ya usa :nombre — renombrar para evitar colisión con SET params
|
||||
foreach ($whereParams as $key => $value) {
|
||||
$newKey = '_w_' . $key;
|
||||
$whereNamed = str_replace(':' . $key, ':' . $newKey, $whereNamed);
|
||||
$namedWhereParams[$newKey] = $value;
|
||||
}
|
||||
} else {
|
||||
// WHERE usa ? posicionales — convertir a nombrados
|
||||
$i = 0;
|
||||
foreach ($whereParams as $value) {
|
||||
$paramName = '_w' . $i;
|
||||
$whereNamed = preg_replace('/\?/', ':' . $paramName, $whereNamed, 1);
|
||||
$namedWhereParams[$paramName] = $value;
|
||||
$i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$sql = "UPDATE {$table} SET {$fieldsStr} WHERE {$whereNamed}";
|
||||
$params = array_merge($data, $namedWhereParams);
|
||||
|
||||
return $this->query($sql, $params);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user