Unable to debug my PHP code!

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>

If you display the list of authors and view the source of the page, is it inserting the correct values for the “id” field?

First, thank you.
Then, to display the list of authors, I need to move the proper code snippet on top of the page, otherwise it won’t show up.
This way, if I check the code of the rendered page I can see that the id field is correctly populated.

I meant the list of authors that it presents, where you have the ‘modify’ and ‘delete’ buttons. Surely it’s displaying that, because the problem is that you get an error when you click on ‘Modify’, or did I read the problem incorrectly?

Change your hidden fields on your forms to ‘text’, you can see if you are capturing the id correctly. If it’s there then you can start looking at why it’s not setting it later on in the code.

There are better ways of doing it (xdebug) but you can also’s use (eg,)

echo 'the id is: '.$id

or for arrays:

var_dump($row)

to quickly output stuff to the screen

You’re right.
I can see the listing page correctly, but only if I place that code snippet ON TOP of the container code. It is normally placed in the bottom. I suppose that I cannot see it (when it is on bottom) because it encounters the exception before, and it stops.

Hi, thanks for your reply too!
As I said before, if I use the code I provided in the first post, even if I set the hidden field to text I just can’t see it, because the page that gets displayed is the “modify author”. It is supposed to show if I click on add, but it gets displayed always, instead.

Ah, I thought the problem was that it displayed the list properly, but when you click “Modify” you get the error.

Try adding this to the top of index.php, just after the include_once:

var_dump($_GET);
var_dump($_POST);

and post the results. It shouldn’t be inside that if condition unless $POST has content.

So, just to be sure we speak the same language:

This is the result you asked.

I got this page when I try to load the index.php page. So, I don’t get the page showing me the authors and what to do with them, I get this instead.

I am uploading the output of the log file I use to “debug”:

MySQL query log:

 150331 14:07:21	   16 Connect	ijdbuser@localhost on ijdb
	   16 Query	SET NAMES "utf8"
	   16 Query	SELECT id, name, email FROM author WHERE id = NULL
	   16 Quit	

PHP log:

 [Tue Mar 31 14:07:21.247177 2015] [:error] [pid 1312] [client 192.168.122.1:47027] PHP Notice:  Undefined index: id in /var/www/html/authors/index.php on line 51, referer: http://192.168.122.41/

I think the problem is that the array is not filled with values: this is happening on another page, also, and I don’t know why!

I think it’s this line:

if (isset($_POST['action']) and $_POST['action'] == 'Modifica');

You’ve got a semicolon on the end of the line which should not be there. So the code that is in the braces and should be controlled by the condition is not, it just executes in line.

It works !
I noticed that error on the other page I told is not working. I removed it from there and not here.
Now I removed it from here and it works correctly, thanks !

Now I must figure out the problem on the other page… it must be another coding typo !

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