This tool will identify and fix all issues with your login history system (Fixed version).
Step 4: Database Operations Test (Fixed)
Testing INSERT operation:
โ INSERT exception: Field 'id' doesn't have a default value
Testing SELECT operation:
โ SELECT failed - no records found
โ Database operations failing
Step 5: Fixed Login History Function Test
Testing fixed recordLoginHistory function:
Function error: Field 'id' doesn't have a default value
โ Login recording failed
Testing failed login recording:
Function error: Field 'id' doesn't have a default value
โ Failed login recording failed
โ Functions still failing
Step 7: Working Function Code
โ
Use this FIXED function in your login files:
<?php
function recordLoginHistory($conn, $user_id, $user_type, $user_name, $action_type = 'login') {
try {
// Check available columns
$structure = $conn->query("DESCRIBE login_history");
$available_columns = [];
if ($structure) {
while ($row = $structure->fetch_assoc()) {
$available_columns[] = $row['Field'];
}
}
// Choose SQL based on available columns
if (in_array('action_type', $available_columns) && in_array('user_agent', $available_columns)) {
$sql = "INSERT INTO login_history (user_id, user_type, user_name, user_agent, action_type) VALUES (?, ?, ?, ?, ?)";
$stmt = $conn->prepare($sql);
if (!$stmt) return false;
$param_user_id = $user_id;
$param_user_type = $user_type;
$param_user_name = $user_name;
$param_user_agent = $_SERVER['HTTP_USER_AGENT'] ?? 'Unknown';
$param_action_type = $action_type;
$stmt->bind_param("sssss", $param_user_id, $param_user_type, $param_user_name, $param_user_agent, $param_action_type);
} elseif (in_array('action_type', $available_columns)) {
$sql = "INSERT INTO login_history (user_id, user_type, user_name, action_type) VALUES (?, ?, ?, ?)";
$stmt = $conn->prepare($sql);
if (!$stmt) return false;
$param_user_id = $user_id;
$param_user_type = $user_type;
$param_user_name = $user_name;
$param_action_type = $action_type;
$stmt->bind_param("ssss", $param_user_id, $param_user_type, $param_user_name, $param_action_type);
} else {
$sql = "INSERT INTO login_history (user_id, user_type, user_name) VALUES (?, ?, ?)";
$stmt = $conn->prepare($sql);
if (!$stmt) return false;
$param_user_id = $user_id;
$param_user_type = $user_type;
$param_user_name = $user_name;
$stmt->bind_param("sss", $param_user_id, $param_user_type, $param_user_name);
}
$result = $stmt->execute();
$stmt->close();
return $result;
} catch (Exception $e) {
error_log("Login history error: " . $e->getMessage());
return false;
}
}
?>