Loading connection.php +24 −15 Original line number Diff line number Diff line <?php session_start(); $dsn = "mysql:host=localhost;dbname=Rubrica;charset=utf8"; try { $dsn = "mysql:host=localhost;dbname=5h_bovo;charset=utf8"; $pdo = new PDO( $dsn, "5h_bovo", Loading @@ -10,12 +10,11 @@ PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_NUM ] ); function getRubrica() { if (!isset($_SESSION["key"])) { return []; } catch (Exception $e) { echo 'Errore trovato: ', $e->getMessage(), "\n"; } function getRubrica() { $query = "SELECT * FROM Rubrica WHERE sessionKey = :key"; $stmt = $pdo->prepare($query); $stmt->bindParam(":key", $_SESSION["key"], PDO::PARAM_STR); Loading @@ -23,4 +22,14 @@ return $stmt->fetchAll(); } function inserisciUtente($nome, $cognome, $numeroTelefono){ $query = "INSERT INTO Rubrica VALUES (:numeroTelefono, :nome, :cognome, :key)"; $stmt = $pdo->prepare($query); $stmt->bindParam(":numeroTelefono", $numeroTelefono, PDO::PARAM_STR); $stmt->bindParam(":nome", $nome, PDO::PARAM_STR); $stmt->bindParam(":cognome", $cognome, PDO::PARAM_STR); $stmt->bindParam(":key", $_SESSION["key"], PDO::PARAM_STR); $stmt->execute(); } ?> No newline at end of file index.php 0 → 100644 +89 −0 Original line number Diff line number Diff line <?php require "connection.php"; session_start(); if (!isset($_SESSION["key"])) { $_SESSION["key"] = bin2hex(random_bytes(16)); } try { if (isset($_POST["nome"]) && isset($_POST["cognome"]) && isset($_POST["telefono"])) { $nome = $_POST["nome"]; $cognome = $_POST["cognome"]; $telefono = $_POST["telefono"]; inserisciUtente($nome, $cognome, $telefono); } } catch (Exception $e) { echo 'Errore trovato: ', $e->getMessage(), "\n"; } ?> <!DOCTYPE html> <html lang="it"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>La Mia Rubrica</title> <style> body { font-family: sans-serif; margin: 20px; line-height: 1.6; } .container { max-width: 800px; margin: auto; } form { background: #f4f4f4; padding: 20px; border-radius: 8px; margin-bottom: 40px; } input { display: block; width: 100%; padding: 10px; margin: 10px 0; border: 1px solid #ccc; border-radius: 4px; box-sizing: border-box; } button { background: #28a745; color: white; border: none; padding: 10px 20px; cursor: pointer; border-radius: 4px; } button:hover { background: #218838; } table { width: 100%; border-collapse: collapse; } th, td { text-align: left; padding: 12px; border-bottom: 1px solid #ddd; } th { background-color: #f8f9fa; } </style> </head> <body> <div class="container"> <h1>Rubrica Contatti</h1> <section> <h2>Aggiungi Nuovo Contatto</h2> <form action="index.php" method="POST"> <input type="text" name="nome" placeholder="Nome" required> <input type="text" name="cognome" placeholder="Cognome" required> <input type="tel" name="telefono" placeholder="Numero di Telefono" required> <button type="submit">Salva Contatto</button> </form> </section> <hr> <section> <h2>Contatti Esistenti</h2> <table> <thead> <tr> <th>Nome</th> <th>Cognome</th> <th>Telefono</th> </tr> </thead> <tbody> <?php require "connection.php"; try { $contatti = getRubrica(); foreach ($contatti as $contatto) { echo "<tr> <td>" . htmlspecialchars($contatto['nome']) . "</td> <td>" . htmlspecialchars($contatto['cognome']) . "</td> <td>" . htmlspecialchars($contatto['telefono']) . "</td> </tr>"; } } catch (Exception $e) { echo 'Errore trovato: ', $e->getMessage(), "\n"; } ?> </tbody> </table> </section> </div> </body> </html> No newline at end of file Loading
connection.php +24 −15 Original line number Diff line number Diff line <?php session_start(); $dsn = "mysql:host=localhost;dbname=Rubrica;charset=utf8"; try { $dsn = "mysql:host=localhost;dbname=5h_bovo;charset=utf8"; $pdo = new PDO( $dsn, "5h_bovo", Loading @@ -10,12 +10,11 @@ PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_NUM ] ); function getRubrica() { if (!isset($_SESSION["key"])) { return []; } catch (Exception $e) { echo 'Errore trovato: ', $e->getMessage(), "\n"; } function getRubrica() { $query = "SELECT * FROM Rubrica WHERE sessionKey = :key"; $stmt = $pdo->prepare($query); $stmt->bindParam(":key", $_SESSION["key"], PDO::PARAM_STR); Loading @@ -23,4 +22,14 @@ return $stmt->fetchAll(); } function inserisciUtente($nome, $cognome, $numeroTelefono){ $query = "INSERT INTO Rubrica VALUES (:numeroTelefono, :nome, :cognome, :key)"; $stmt = $pdo->prepare($query); $stmt->bindParam(":numeroTelefono", $numeroTelefono, PDO::PARAM_STR); $stmt->bindParam(":nome", $nome, PDO::PARAM_STR); $stmt->bindParam(":cognome", $cognome, PDO::PARAM_STR); $stmt->bindParam(":key", $_SESSION["key"], PDO::PARAM_STR); $stmt->execute(); } ?> No newline at end of file
index.php 0 → 100644 +89 −0 Original line number Diff line number Diff line <?php require "connection.php"; session_start(); if (!isset($_SESSION["key"])) { $_SESSION["key"] = bin2hex(random_bytes(16)); } try { if (isset($_POST["nome"]) && isset($_POST["cognome"]) && isset($_POST["telefono"])) { $nome = $_POST["nome"]; $cognome = $_POST["cognome"]; $telefono = $_POST["telefono"]; inserisciUtente($nome, $cognome, $telefono); } } catch (Exception $e) { echo 'Errore trovato: ', $e->getMessage(), "\n"; } ?> <!DOCTYPE html> <html lang="it"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>La Mia Rubrica</title> <style> body { font-family: sans-serif; margin: 20px; line-height: 1.6; } .container { max-width: 800px; margin: auto; } form { background: #f4f4f4; padding: 20px; border-radius: 8px; margin-bottom: 40px; } input { display: block; width: 100%; padding: 10px; margin: 10px 0; border: 1px solid #ccc; border-radius: 4px; box-sizing: border-box; } button { background: #28a745; color: white; border: none; padding: 10px 20px; cursor: pointer; border-radius: 4px; } button:hover { background: #218838; } table { width: 100%; border-collapse: collapse; } th, td { text-align: left; padding: 12px; border-bottom: 1px solid #ddd; } th { background-color: #f8f9fa; } </style> </head> <body> <div class="container"> <h1>Rubrica Contatti</h1> <section> <h2>Aggiungi Nuovo Contatto</h2> <form action="index.php" method="POST"> <input type="text" name="nome" placeholder="Nome" required> <input type="text" name="cognome" placeholder="Cognome" required> <input type="tel" name="telefono" placeholder="Numero di Telefono" required> <button type="submit">Salva Contatto</button> </form> </section> <hr> <section> <h2>Contatti Esistenti</h2> <table> <thead> <tr> <th>Nome</th> <th>Cognome</th> <th>Telefono</th> </tr> </thead> <tbody> <?php require "connection.php"; try { $contatti = getRubrica(); foreach ($contatti as $contatto) { echo "<tr> <td>" . htmlspecialchars($contatto['nome']) . "</td> <td>" . htmlspecialchars($contatto['cognome']) . "</td> <td>" . htmlspecialchars($contatto['telefono']) . "</td> </tr>"; } } catch (Exception $e) { echo 'Errore trovato: ', $e->getMessage(), "\n"; } ?> </tbody> </table> </section> </div> </body> </html> No newline at end of file