Hello,
I am reading the book “PHP & MySQL: Novice To Ninja, 5th Edition”.
I am new to PHP and I’m not able to make it work as intended.
Unfortunately I can’t upload files 'cause I’m new. The code is pretty long, so forgive me for the lenght of this post.
The container “index.php” is not working properly: I want it to show the list of authors with two buttons for delete and modify. I can add new authors but I can’t delete or modify.
The PHP error log tells me on line 50 of index.php: Undefined index: id
I can’t figure out what is wrong, I suppose it is a folishness but I can’t dubug it out !
Thanks for your reply.
INDEX.PHP
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/includes/magicquotes.inc.php';
if (isset($_GET['add']))
{
$pageTitle = 'Nuovo autore';
$action = 'addform';
$name = '';
$email = '';
$id = '';
$button = 'Aggiungi autore';
include 'form.html.php';
exit();
}
if (isset($_GET['addform']))
{
include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
try
{
$sql = 'INSERT INTO author SET
name = :name,
email = :email';
$s = $pdo->prepare($sql);
$s->bindValue(':name', $_POST['name']);
$s->bindValue(':email', $_POST['email']);
$s->execute();
}
catch (PDOException $e)
{
$error = 'Errore in aggiunta autore';
include 'error.html.php';
exit();
}
header('Location: .');
exit();
}
if (isset($_POST['action']) and $_POST['action'] == 'Modifica');
{
include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
try
{
$sql = 'SELECT id, name, email FROM author WHERE id = :id';
$s = $pdo->prepare($sql);
$s->bindValue(':id', $_POST['id']);
$s->execute();
}
catch (PDOException $e)
{
$error = 'Errore in recupero dati autore';
include 'error.html.php';
exit();
}
$row = $s->fetch();
$pageTitle = 'Modifica autore';
$action = 'editform';
$name = $row['name'];
$email = $row['email'];
$id = $row['id'];
$button = 'Aggiorna autore';
include 'form.html.php';
exit();
}
if (isset($_GET['editform']))
{
include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
try
{
$sql = 'UPDATE author SET
name = :name,
email = :email
WHERE id = :id';
$s = $pdo->prepare($sql);
$s->bindValue(':id', $_POST['id']);
$s->bindValue(':name', $_POST['name']);
$s->bindValue(':email', $_POST['email']);
$s->execute();
}
catch (PDOException $e)
{
$error = 'Errore in aggiornamento autore';
include 'error.html.php';
exit();
}
header('Location: .');
exit();
}
if (isset($_POST['action']) and $_POST['action'] == 'Elimina')
{
include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
// Ottenere i joke appartenente a specifico autore
try
{
$sql = 'SELECT id FROM joke WHERE authorid = :id';
$s = $pdo->prepare($sql);
$s->bindValue(':id', $_POST['id']);
$s->execute();
}
catch (PDOException $e)
{
$error = 'Errore nel recuperare i joke da eliminare !';
include 'error.html.php';
exit();
}
$result = $s->fetchAll();
// Elimina voci delle categorie joke
try
{
$sql = 'DELETE FROM jokecategory WHERE jokeid = :id';
$s = $pdo->prepare($sql);
// Per ogni joke
foreach ($result as $row)
{
$jokeId = $row['id'];
$s->bindValue(':id', $jokeId);
$s->execute();
}
}
catch (PDOException $e)
{
$error = 'Errore in eliminazione voci delle categorie di joke';
include 'error.html.php';
exit();
}
// Elimina i joke appartenenti all'autore
try
{
$sql = 'DELETE FROM joke WHERE authorid = :id';
$s = $pdo->prepare($sql);
$s->bindValue(':id', $_POST['id']);
$s->execute();
}
catch (PDOException $e)
{
$error = 'Errore nella cancellazione dei joke per autore';
include 'error.html.php';
exit();
}
// Elimina l'autore
try
{
$sql = 'DELETE FROM author WHERE id = :id';
$s = $pdo->prepare($sql);
$s->bindValue(':id', $_POST['id']);
$s->execute();
}
catch (PDOException $e)
{
$error = 'Errore in eliminazione autore';
include 'error.html.php';
exit();
}
header('Location: .');
exit();
}
// Visualizza elenco autori
include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
try
{
$result = $pdo->query('SELECT id, name FROM author');
}
catch (PDOException $e)
{
$error = 'Errore nel recupero della lista autori';
include 'error.html.php';
exit();
}
foreach ($result as $row)
{
$authors[] = array('id' => $row['id'], 'name' => $row['name']);
}
include 'authors.html.php';
AUTHORS.HTML.PHP:
<?php include_once $_SERVER['DOCUMENT_ROOT'] . '/includes/helpers.inc.php'; ?>
<DOCTYPE html>
<html lang="it">
<head>
<meta charset="utf-8">
<title>Gestione autori</title>
</head>
<body>
<h1>Gestione autori</h1>
<p><a href="?add">Aggiungi nuovo autore</a></p>
<ul>
<?php foreach ($authors as $author): ?>
<li>
<form action="" method="post">
<div>
<?php htmlout($author['name']); ?>
<input type="hidden" name="id" value="<?php echo $author['id']; ?>">
<input type="submit" name="action" value="Modifica">
<input type="submit" name="action" value="Elimina">
</div>
</form>
</li>
<?php endforeach; ?>
</ul>
<p><a href="..">Torna alla prima pagina JMS</a></p>
</body>
</html>
FORM.HTML.PHP:
<?php include_once $_SERVER['DOCUMENT_ROOT'] . '/includes/helpers.inc.php'; ?>
<!DOCTYPE html>
<html lang="it">
<head>
<meta charset="utf-8">
<title><?php htmlout($pageTitle); ?></title>
</head>
<body>
<h1><?php htmlout($pageTitle); ?></h1>
<form action="?<?php htmlout($action); ?>" method="post">
<div>
<label for="name">Nome: <input type="text" name="name" id="name" value="<?php htmlout($name); ?>"></label>
</div>
<div>
<label for="email">Email: <input type="text" name="email" id="email" value="<?php htmlout($email); ?>"></label>
</div>
<div>
<input type="hidden" name="id" value="<?php htmlout($id); ?>">
<input type="submit" value="<?php htmlout($button); ?>">
</div>
</form>
</body>
</html>