Files
cinema/file/add/index.php
Francesco Mancuso f2b3b9acc8
All checks were successful
Deploy / trigger (push) Successful in 2s
Conversione da MySQLi a PDO - pt. 2
2026-04-12 20:31:39 +02:00

144 lines
4.1 KiB
PHP

<?php
if (isset($_GET['s'])) {
highlight_file("index.php");
return;
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Dati inviati
$checks = true;
$nome = "";
$cognome = "";
$data_nascita = "";
// htmlspecialchars effettua una sanitizzazione base
if (isset($_POST['nome'])) {
if ($_POST['nome'] === "" || $_POST['nome'] === null) {
$checks = false;
}
$nome = htmlspecialchars($_POST['nome']);
} else {
$checks = false;
}
if (isset($_POST['cognome'])) {
if ($_POST['cognome'] === "" || $_POST['cognome'] === null) {
$checks = false;
}
$cognome = htmlspecialchars($_POST['cognome']);
} else {
$checks = false;
}
if (isset($_POST['data-nascita'])) {
if ($_POST['data-nascita'] === "" || $_POST['data-nascita'] === null) {
$checks = false;
}
$data_nascita = date("Y-m-d H:i:s", strtotime(htmlspecialchars($_POST['data-nascita'])));
} else {
$checks = false;
}
if (!$checks) {
// Codice HTTP 400: Bad Request
http_response_code(400);
echo '<code>Dati mancanti nella richiesta POST!</code>';
return;
}
// Questi valori vengono letti automaticamente
$host = getenv('DB_HOST'); // Sarà "local_db"
$db = getenv('DB_NAME'); // Sarà "nomerepository-nomeutente"
$user = getenv('DB_USER'); // Uguale al nome DB
$pass = getenv('DB_PASS'); // Password generata casualmente ad ogni deploy
$charset = 'utf8mb4';
$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
try {
$pdo = new PDO($dsn, $user, $pass);
} catch (\PDOException $e) {
throw new \PDOException($e->getMessage(), (int) $e->getCode());
}
// Collegamento al DB effettuato, ora facciamo l'inserimento
$insert = "INSERT INTO attori (nome, cognome, data_nascita) VALUES (':nome',':cognome', ':data')";
$stmt = $pdo->prepare($insert);
$stmt->execute([
':nome' => $nome,
':cognome' => $cognome,
':data' => $data_nascita
]);
}
?>
<!DOCTYPE html>
<html lang="it">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Aggiungi attore - The NoSpace Cinema</title>
<!--- www.francescomancuso.it -->
<link rel="stylesheet" href="../style.css">
</head>
<body>
<header>
<div class="container">
<a href="../" style="text-decoration: none;">
<div>
<img src="../logo.png">
<h4>The NoSpace Cinema</h4>
</div>
</a>
<div>
<a href=".?s" style="font-size:16px">Vedi sorgente pagina</a>
<a href="../search/" class="button">Tutti i film</a>
</div>
</div>
</header>
<div class="container">
<div class="hero">
<h1>Aggiungi attore</h1>
</div>
</div>
<div class="page">
<div class="container">
<?php if ($_SERVER['REQUEST_METHOD'] === 'POST') {
echo "<div class=\"message\">Query eseguita con successo!</div>";
} ?>
<form action="." method="POST">
<fieldset>
<label for="nome">Nome</label>
<input type="text" id="nome" name="nome">
</fieldset>
<fieldset>
<label for="cognome">Cognome</label>
<input type="text" id="cognome" name="cognome">
</fieldset>
<fieldset>
<label for="data-nascita">Data di nascita</label>
<input type="date" id="data-nascita" name="data-nascita" max="0">
</fieldset>
<fieldset>
<button type="submit">Invia</button>
</fieldset>
</form>
</div>
</div>
<footer>
<div class="container">
<span>Realizzato da Francesco Giuseppe Mancuso - classe 5E - www.francescomancuso.it</span>
</div>
</footer>
</body>
</html>