generated from francesco/deploy-dinamico
Conversione da MySQLi a PDO - pt. 2
All checks were successful
Deploy / trigger (push) Successful in 2s
All checks were successful
Deploy / trigger (push) Successful in 2s
This commit is contained in:
@@ -50,27 +50,31 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
}
|
||||
|
||||
|
||||
$conn = mysqli_connect("localhost", "root", "", "film_attori");
|
||||
// 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';
|
||||
|
||||
if ($conn === false) {
|
||||
exit("<code>Errore: impossibile stabilire una connessione " . mysqli_connect_error() . "</code>");
|
||||
$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());
|
||||
}
|
||||
|
||||
//echo "<code>Connesso: " . mysqli_get_host_info($conn) . "</code>";
|
||||
|
||||
// Collegamento al DB effettuato, ora facciamo l'inserimento
|
||||
|
||||
$insert = "INSERT INTO attori (nome, cognome, data_nascita) VALUES ('" . $nome . "','" . $cognome . "', '" . $data_nascita . "')";
|
||||
$insert = "INSERT INTO attori (nome, cognome, data_nascita) VALUES (':nome',':cognome', ':data')";
|
||||
|
||||
if (mysqli_query($conn, $insert) === false) {
|
||||
// Codice HTTP 500: Errore
|
||||
http_response_code(500);
|
||||
echo "<code>Errore: impossibile eseguire la query. " . mysqli_error($conn) . "</code>";
|
||||
mysqli_close($conn);
|
||||
exit();
|
||||
}
|
||||
|
||||
mysqli_close($conn);
|
||||
$stmt = $pdo->prepare($insert);
|
||||
$stmt->execute([
|
||||
':nome' => $nome,
|
||||
':cognome' => $cognome,
|
||||
':data' => $data_nascita
|
||||
]);
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
Reference in New Issue
Block a user