Кемеровские программисты php общаются здесь
Anonymous
Сообщение об ошибке повторяющегося первичного ключа не отображается
Сообщение
Anonymous » 26 июн 2024, 13:57
Когда я добавляю новую строку и помещаю что-то в уже существующее поле первичного ключа, в моем файле error.log выдается исключение, но на веб-сайте все равно отображается сообщение «Строка добавлена успешно» (на немецком языке).< /p>
Код: Выделить всё
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["addRow"])) {
$tableName = isset($_POST["tableName"]) ? trim($_POST["tableName"]) : '';
$columnsResult = $conn->query("SHOW COLUMNS FROM $tableName");
$columns = [];
$valuesArray = [];
while ($column = $columnsResult->fetch_assoc()) {
$columns[] = $column['Field'];
$valuesArray[] = isset($_POST[$column['Field']]) ? trim($_POST[$column['Field']]) : '';
}
$columnsString = implode(", ", $columns);
$placeholders = implode(", ", array_fill(0, count($columns), "?"));
$insertQuery = "INSERT INTO $tableName ($columnsString) VALUES ($placeholders)";
$stmt = $conn->prepare($insertQuery);
$types = str_repeat("s", count($valuesArray));
$stmt->bind_param($types, ...$valuesArray);
try {
$stmt->execute();
$successMessage = 'Neue Zeile erfolgreich hinzugefügt!';
} catch (mysqli_sql_exception $e) {
if ($e->getCode() == 1062) { // Duplicate entry error code
$errorMessage = 'Fehler beim Hinzufügen der Zeile: Eintrag mit demselben Primärschlüssel existiert bereits.';
} else {
$errorMessage = 'Fehler beim Hinzufügen der Zeile: ' . $e->getMessage();
}
error_log($e->getMessage()); // Log the error message
} finally {
$stmt->close();
}
}
В коде уже показано, что я пробовал: перехватить исключение mysqli SQL.
Подробнее здесь:
https://stackoverflow.com/questions/786 ... sage-shown
1719399426
Anonymous
Когда я добавляю новую строку и помещаю что-то в уже существующее поле первичного ключа, в моем файле error.log выдается исключение, но на веб-сайте все равно отображается сообщение «Строка добавлена успешно» (на немецком языке).< /p> [code]if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["addRow"])) { $tableName = isset($_POST["tableName"]) ? trim($_POST["tableName"]) : ''; $columnsResult = $conn->query("SHOW COLUMNS FROM $tableName"); $columns = []; $valuesArray = []; while ($column = $columnsResult->fetch_assoc()) { $columns[] = $column['Field']; $valuesArray[] = isset($_POST[$column['Field']]) ? trim($_POST[$column['Field']]) : ''; } $columnsString = implode(", ", $columns); $placeholders = implode(", ", array_fill(0, count($columns), "?")); $insertQuery = "INSERT INTO $tableName ($columnsString) VALUES ($placeholders)"; $stmt = $conn->prepare($insertQuery); $types = str_repeat("s", count($valuesArray)); $stmt->bind_param($types, ...$valuesArray); try { $stmt->execute(); $successMessage = 'Neue Zeile erfolgreich hinzugefügt!'; } catch (mysqli_sql_exception $e) { if ($e->getCode() == 1062) { // Duplicate entry error code $errorMessage = 'Fehler beim Hinzufügen der Zeile: Eintrag mit demselben Primärschlüssel existiert bereits.'; } else { $errorMessage = 'Fehler beim Hinzufügen der Zeile: ' . $e->getMessage(); } error_log($e->getMessage()); // Log the error message } finally { $stmt->close(); } } [/code] В коде уже показано, что я пробовал: перехватить исключение mysqli SQL. Подробнее здесь: [url]https://stackoverflow.com/questions/78671363/no-duplicate-primary-key-error-message-shown[/url]