Good evening,
These days I’m studying PHP to learn this programming language. Unfortunately, I got into a mistake and I do not know how to deal with it. Is it possible to explain where I’m wrong? Thank you.
index.php:
<?php
if (isset($_GET['formnomi'])) {
include 'formnomi.html.php';
exit();
}
try {
$connect = new PDO('mysql:host=localhost; dbname=utenti', 'root', 'password,123');
$connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$connect->exec('SET NAMES "utf8"');
} catch (PDOException $e) {
$error = "Impossibile connettersi al database: " . $e;
echo $error;
exit();
}
if (isset($_POST['nome'])){
try {
$sql = 'INSERT INTO utenti SET nome = :nome, id = :id';
$s = $connect->prepare($sql);
$s->bindValue(':nome', $_POST['nome']);
$s->execute();
} catch (PDOException $e) {
$error = 'Impossibile inserire il nome: ' . $e;
echo $error;
exit();
}
header('Location: .');
exit();
}
try {
$sql = 'SELECT id, nome FROM utenti';
$result = $connect->query($sql);
} catch (PDOException $e){
$error = "Impossibile recuperare la lista di nomi: " . $e;
echo $error;
exit();
}
while ($utenti = $result->fetch()) {
$lista[] = array(':id' => $utenti['id'], ':nome' => $utenti['nome']);
}
include 'form.html.php';
?>
form.html.php:
<html>
<head>
<meta charset="utf-8">
<title>Nomi inseriti</title>
</head>
<body>
<center>
<a href="?formnomi">Inserisci nomi</a>
<p><h2>Ecco tutti i nomi inseriti:</h2></p>
<?php foreach ($lista as $listas): ?>
<form action="?delete" method="post">
<?php echo htmlspecialchars($listas['nome'], ENT_QUOTES, 'UTF-8');?>
<input type="hidden" name="id" value="<?php echo $listas['id'];?>" />
<input type="submit" value="Elimina" />
</form>
<?php endforeach; ?>
</center>
</body>
</html>
formnomi.html.php:
<html>
<head>
<title>Aggiungi nome...</title>
</head>
<body>
<form method="post" action="?">
<center>
<p>Inserisci il tuo nome e clicca "Invia" per confermare</p>
<br />
<input class="text" type="text" id="nome" name="nome" value="Inserisci il tuo nome">
<br />
<br />
<input type="submit">
<br />
<br />
<a href=".">Torna alla pagina precedente</a>
</center>
</form>
</body>
</html>
the browser shows me this:
Thanks !!