Waddah
Waddah
KPCKevin Powell - Community
Created by Waddah on 1/31/2024 in #back-end
Newbie in back end
@Skylark Thanks for helping me!
22 replies
KPCKevin Powell - Community
Created by Waddah on 1/31/2024 in #back-end
Newbie in back end
Im gonna die please help )::::
22 replies
KPCKevin Powell - Community
Created by Waddah on 1/31/2024 in #back-end
Newbie in back end
<?php
session_start();

$host = 'localhost';
$db = 'website';
$user = 'root';
$pass = '';
$charset = 'utf8mb4';

$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];

$response = []; // Initialize an empty response array

try {
$pdo = new PDO($dsn, $user, $pass, $options);
} catch (\PDOException $e) {
$response['error'] = $e->getMessage();
echo json_encode($response);
exit;
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$fname = $_POST['first_name'] ?? '';
$lname = $_POST['last_name'] ?? '';
$petname = $_POST['pet_name'] ?? '';
$email = $_POST['email'] ?? '';
$password = $_POST['password'] ?? '';
$cpassword = $_POST['confirm_password'] ?? '';

// Validate input and check if passwords match
if (empty($fname) || empty($lname) || empty($email) || empty($password) || empty($cpassword) || $password != $cpassword) {
$response['error'] = 'Invalid input or passwords do not match';
} else {
// Check if the email already exists in the database
$stmt = $pdo->prepare("SELECT COUNT(*) FROM users WHERE email = ?");
$stmt->execute([$email]);
$emailExists = (bool)$stmt->fetchColumn();

if ($emailExists) {
// Email already exists, set email_exists to true in the response
$response['email_exists'] = true;
} else {
// Email is available, proceed with insertion
$hashed_password = password_hash($password, PASSWORD_DEFAULT);

// File upload handling for pet image
if (isset($_FILES['pimg']) && $_FILES['pimg']['error'] === UPLOAD_ERR_OK) {
$imgData = file_get_contents($_FILES['pimg']['tmp_name']);

$stmt = $pdo->prepare("INSERT INTO users (fname, lname, petname, email, password, pimg) VALUES (?, ?, ?, ?, ?, ?)");
$stmt->execute([$fname, $lname, $petname, $email, $hashed_password, $imgData]);

// Set session variable and cookie
$_SESSION['userName'] = $fname;
setcookie('userName', $fname, time() + (86400 * 30), "/"); // Set cookie for 30 days

$response['success'] = 'Registration successful';
} else {
$response['error'] = 'Error in file upload';
}
}
}

// Return the response as JSON
echo json_encode($response);
}
?>
<?php
session_start();

$host = 'localhost';
$db = 'website';
$user = 'root';
$pass = '';
$charset = 'utf8mb4';

$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];

$response = []; // Initialize an empty response array

try {
$pdo = new PDO($dsn, $user, $pass, $options);
} catch (\PDOException $e) {
$response['error'] = $e->getMessage();
echo json_encode($response);
exit;
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$fname = $_POST['first_name'] ?? '';
$lname = $_POST['last_name'] ?? '';
$petname = $_POST['pet_name'] ?? '';
$email = $_POST['email'] ?? '';
$password = $_POST['password'] ?? '';
$cpassword = $_POST['confirm_password'] ?? '';

// Validate input and check if passwords match
if (empty($fname) || empty($lname) || empty($email) || empty($password) || empty($cpassword) || $password != $cpassword) {
$response['error'] = 'Invalid input or passwords do not match';
} else {
// Check if the email already exists in the database
$stmt = $pdo->prepare("SELECT COUNT(*) FROM users WHERE email = ?");
$stmt->execute([$email]);
$emailExists = (bool)$stmt->fetchColumn();

if ($emailExists) {
// Email already exists, set email_exists to true in the response
$response['email_exists'] = true;
} else {
// Email is available, proceed with insertion
$hashed_password = password_hash($password, PASSWORD_DEFAULT);

// File upload handling for pet image
if (isset($_FILES['pimg']) && $_FILES['pimg']['error'] === UPLOAD_ERR_OK) {
$imgData = file_get_contents($_FILES['pimg']['tmp_name']);

$stmt = $pdo->prepare("INSERT INTO users (fname, lname, petname, email, password, pimg) VALUES (?, ?, ?, ?, ?, ?)");
$stmt->execute([$fname, $lname, $petname, $email, $hashed_password, $imgData]);

// Set session variable and cookie
$_SESSION['userName'] = $fname;
setcookie('userName', $fname, time() + (86400 * 30), "/"); // Set cookie for 30 days

$response['success'] = 'Registration successful';
} else {
$response['error'] = 'Error in file upload';
}
}
}

// Return the response as JSON
echo json_encode($response);
}
?>
22 replies
KPCKevin Powell - Community
Created by Waddah on 1/31/2024 in #back-end
Newbie in back end
function checkEmailExists(email) {
// Send an AJAX request to check if the email already exists
fetch('register.php', {
method: 'POST',
body: JSON.stringify({ email: email }),
headers: {
'Content-Type': 'application/json'
},
})
.then(response => response.json()) // Parse JSON response
.then(data => {

if (data.email_exists) {
// Email already exists, display an error message
const errorMessage = document.querySelector('.message');
errorMessage.textContent = 'Email is already registered.';
errorMessage.style.color = "red";
document.querySelector('input[name="email"]').classList.add('shake');
document.querySelector('input[name="email"]').value = "";

setTimeout(function () {
// Restore the original message
document.querySelector('input[name="email"]').classList.remove('shake');
errorMessage.textContent = "Signup now and get full access to our app.";
errorMessage.style.color = "";
}, 2000);
} else if (data.error) {
// Handle other errors if necessary
console.error('Error:', data.error);

} else {
// Email is available, continue with the registration
document.querySelector('.form').submit(); // Submit the form
}
})
.catch(error => {
console.error('Error:', error);
});
}
function checkEmailExists(email) {
// Send an AJAX request to check if the email already exists
fetch('register.php', {
method: 'POST',
body: JSON.stringify({ email: email }),
headers: {
'Content-Type': 'application/json'
},
})
.then(response => response.json()) // Parse JSON response
.then(data => {

if (data.email_exists) {
// Email already exists, display an error message
const errorMessage = document.querySelector('.message');
errorMessage.textContent = 'Email is already registered.';
errorMessage.style.color = "red";
document.querySelector('input[name="email"]').classList.add('shake');
document.querySelector('input[name="email"]').value = "";

setTimeout(function () {
// Restore the original message
document.querySelector('input[name="email"]').classList.remove('shake');
errorMessage.textContent = "Signup now and get full access to our app.";
errorMessage.style.color = "";
}, 2000);
} else if (data.error) {
// Handle other errors if necessary
console.error('Error:', data.error);

} else {
// Email is available, continue with the registration
document.querySelector('.form').submit(); // Submit the form
}
})
.catch(error => {
console.error('Error:', error);
});
}
22 replies
KPCKevin Powell - Community
Created by Waddah on 1/31/2024 in #back-end
Newbie in back end
I dont know what im doing ):
22 replies
KPCKevin Powell - Community
Created by Waddah on 1/31/2024 in #back-end
Newbie in back end
I can provide all files if someone wants to check it out
22 replies