Error PHP "Undefined index"

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 !!

Well, the Error is telling you where the problem is and what it is. “Undefined index: nome”, on line 12.

Start at the top of your PHP file, and count to 12.

			<?php echo htmlspecialchars($listas['nome'], ENT_QUOTES, 'UTF-8');?>

So listas doesnt contain an index called “nome”. Which is true. You didnt call your index “nome”.

		$lista[] = array(':id' => $utenti['id'], ':nome' => $utenti['nome']);

You called your index value “:nome”, not “nome”.

Thank you! I did not understand the mistake and now I solved it. You have been very kind and I hope to learn more about this programming language! Thank you!

1 Like

May I suggest adding error_reporting(), etc shown in following script to the top of first called PHP file. All the settings will also be applied to any included files.

The reason for error_reporting is it is best to know of all errors and warnings which may not prevent the script from running but could possibly occur later, Best to know in advance rather than late Friday afternoon :slight_smile:

<?php 
  declare(strict_types=1); // ONLY FOR PHP7 
  ini_set('html_errors', 'true');
  ini_set('display_errors', 'true');
  ini_set('display_startup_errors', 'true');
  error_reporting(-1);
  // FORCE ERROR TEST
     echo $x/$y;

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.