Fixed functionality issue for profiles
This commit is contained in:
41
add_profile.php
Normal file
41
add_profile.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
require 'db.php';
|
||||
require 'auth.php';
|
||||
|
||||
header('Content-Type: application/json');
|
||||
require_login();
|
||||
|
||||
$user_id = current_user_id();
|
||||
|
||||
$data = json_decode(file_get_contents('php://input'), true);
|
||||
$name = trim($data['name'] ?? '');
|
||||
|
||||
if ($name === '') {
|
||||
http_response_code(400);
|
||||
echo json_encode(['success' => false, 'error' => 'Profile name is required']);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Prevent duplicate profile names per user
|
||||
$stmt = $pdo->prepare("SELECT id FROM profiles WHERE user_id = ? AND name = ?");
|
||||
$stmt->execute([$user_id, $name]);
|
||||
|
||||
if ($stmt->fetchColumn()) {
|
||||
http_response_code(409);
|
||||
echo json_encode(['success' => false, 'error' => 'Profile already exists']);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Insert new profile
|
||||
$stmt = $pdo->prepare("INSERT INTO profiles (user_id, name, is_default) VALUES (?, ?, 0)");
|
||||
$stmt->execute([$user_id, $name]);
|
||||
|
||||
$newProfileId = $pdo->lastInsertId();
|
||||
|
||||
// Make it active immediately
|
||||
$_SESSION['active_profile_id'] = (int)$newProfileId;
|
||||
|
||||
echo json_encode([
|
||||
'success' => true,
|
||||
'profile_id' => $newProfileId
|
||||
]);
|
||||
Reference in New Issue
Block a user