Files
Progetto-PHP/PHP/register_action.php
T
Emanuele Slusarz ec30fb40b9 first commit
2026-08-22 11:15:29 +02:00

113 lines
2.5 KiB
PHP
Executable File

<!--
QUI VIENE IMPLEMENTATA LA REGISTRAZIONE:
UTENTE + CLIENTE
-->
<?php
require_once __DIR__ . "/config/database.php";
if ($_SERVER["REQUEST_METHOD"] !== "POST"){
header("Location: registrazione.php");
exit;
}
$nome = trim($_POST["nome"] ?? "");
$cognome = trim($_POST["cognome"] ?? "");
$email = trim($_POST["email"] ?? "");
$telefono = trim($_POST["telefono"] ?? "");
$password = trim($_POST["password"] ?? "");
$confermaPassword = trim($_POST["confermaPassword"] ?? "");
$via = trim($_POST["via"] ?? "");
$numeroCivico = trim($_POST["numeroCivico"] ?? "");
$idComune = filter_input(INPUT_POST, "idComune", FILTER_VALIDATE_INT);
// Controllo che i dati in input non siano vuoti
if (
$nome === "" ||
$cognome === "" ||
$email === "" ||
$telefono === "" ||
$password === "" ||
$via === "" ||
$numeroCivico === "" ||
!$idComune
) {
die("Compila tutti i campi.");
}
// Controllo che le due password siano identiche
if ($password !== $confermaPassword) {
die("Le passoword non corrispondono.");
}
// Hash delle password
$passwordHash = password_hash($password, PASSWORD_DEFAULT);
try {
$pdo->beginTransaction();
// Creazione dell'utente
$stmt = $pdo->prepare("
INSERT INTO utente (nome, cognome, email, passwordHash, attivo)
VALUES (:nome, :cognome, :email, :passwordHash, 1)
");
$stmt->execute([
"nome" => $nome,
"cognome" => $cognome,
"email" => $email,
"passwordHash" => $passwordHash
]);
/*
In seguito all'aggiunta dell'utente
recuperiamo l'ultimo elemento aggiunto
in modo da linkarlo alla tabella della
clientela
*/
$idUtente = $pdo->lastInsertId();
// Creazione del cliente
$stmt = $pdo->prepare("
INSERT INTO cliente (idUtente, telefono)
VALUES (:idUtente, :telefono)
");
$stmt->execute([
"idUtente" => $idUtente,
"telefono" => $telefono
]);
$stmt = $pdo->prepare("
INSERT INTO indirizzo (via, numeroCivico, predefinito, idComune, idCliente)
VALUES (:via, :numeroCivico, 1, :idComune, :idUtente)
");
$stmt->execute([
"via" => $via,
"numeroCivico" => $numeroCivico,
"idComune" => $idComune,
"idUtente" => $idUtente
]);
$pdo->commit();
header("Location: index.php?registrazione=ok");
exit;
} catch (PDOException $e) {
if ($pdo->inTransaction()) {
$pdo->rollBack();
}
die(
"Errore durante la registrazione: "
. $e->getMessage()
);
}