No result from fetchAll();

Hi,

I’m totally a newbie in the world of PHP and MySQL. I am reading the book of Kevin Yank and while doing one of his examples, I encountered a weird outcome. I’m sure I followed and typed correctly the code written on his book but I wondered why I’m not getting the same result. I’ve checked the code back and forth and sure that it is right or maybe I’m missing something.

I’m posting this here because I know a lot from here are very helpful and very kind to starters like me. Any inputs are highly appreciated. Looking forward for comments so that I can proceed with my studies because I think I am stuck because of this weird error.

Please see below code for your reference.

index.html

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="utf-8">
	<title>Joke CMS</title>
</head>
<body>
	<h1>Joke Management System</h1>
	<ul>
		<li><a href="jokes/">Manage Jokes</a></li>
		<li><a href="authors/">Manage Authors</a></li>
		<li><a href="categories/">Manage Joke Categories</a></li>
	</ul>
</body>
</html>

index.php

<?php
if (get_magic_quotes_gpc())
{
$process = array(&$_GET, &$_POST, &$_COOKIE, &$_REQUEST);
while (list($key, $val) = each($process))
{
foreach ($val as $k => $v)
{
unset($process[$key][$k]);
if (is_array($v))
{
$process[$key][stripslashes($k)] = $v;
$process[] = &$process[$key][stripslashes($k)];
}
else
{
$process[$key][stripslashes($k)] = stripslashes($v);
}
}
}
unset($process);
}

//Display author list
include_once $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
try {
	$result = $pdo->query('SELECT id, name FROM author');
} catch (PDOException $e){
	$error = 'Error fetching authors from database! ' . $e->getMessage();
	include 'error.html.php';
	exit();
} 

if (isset($_POST['action']) and $_POST['action'] == 'Delete')
	{
		include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
		//Get jokes belonging to author
		try {
			$sql = 'SELECT id FROM joke WHERE authorid = :id';
			$s = $pdo->prepare($sql);
			$s->bindValue(':id', $_POST['id']);
			$s->execute();
		} catch (PDOException $e){
			$error = 'Error fetching authors with their jokes! ' . $e->getMessage();
			include 'error.html.php';
			exit();
		}
		
		$result = $s->fetchAll();

		//Delete joke category entries
		try {
			$sql = 'DELETE FROM jokecategory WHERE jokeid = :id';
			$s = $pdo->prepare($sql);

			//For each joke
			foreach ($result as $row)
				{
					$jokeId = $row['id'];
					$s->bindValue(':id', $jokeId);
					$s->execute();
				}
		} catch (PDOException $e){
			$error = 'Error deleting joke category! ' . $e->getMessage();
			include 'error.html.php';
			exit();
		}

		//Delete jokes belonging to author
		try {
			$sql = 'DELETE FROM joke WHERE authorid = :id';
			$s = $pdo->prepare($sql);
			$s->bindValue(':id', $_POST['id']);
			$s->execute();
		} catch (PDOException $e){
			$error = 'Error deleting joke from a specific author! ' . $e->getMessage();
			include 'error.html.php';
			exit();
		}

		//Delete the author
		try {
			$sql = 'DELETE FROM author WHERE id = :id';
			$s = $pdo->prepare($sql);
			$s->bindValue(':id', $_POST['id']);
			$s->execute();
		} catch (PDOException $e){
			$error = 'Error deleting the author from database! ' . $e->getMessage();
			include 'error.html.php';
			exit();
		}

		header('Location: .');
		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/helper.inc.php';
?>
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="utf-8"/>
	<title>Manage Authors</title>
</head>
<body>
	<h1>Manage Authors</h1>
	<p><a href="?add">Add new author</a></p>
	<ul>
		<?php foreach($authors as $author): ?>
			<li>
				<form action="" method="post">
					<div>
						<?php echo htmlout($author['name']); ?>
						<input type="hidden" value="<?php echo htmlout($author['id']); ?>"/>
						<input type="submit" name="action" value="Edit"/>
						<input type="submit" name="action" value="Delete"/>
					</div>
				</form>
			</li>
		<?php endforeach; ?>
	</ul>
	<p><a href="..">Return to JMS home</a></p>
</body>
</html>

Best regards,
Karl

What seems to be the problem?

The problem is that when I click the “Delete” button, it would not delete the author entry. I am sure I typed it right and wondering why I’m not getting the desired result. I am hoping that someone could help me resolve this one.

Without getting all into this, aren’t you missing the name=“id” here?

<input type="hidden" value="<?php echo htmlout($author['id']); ?>"/>

Add these lines after the <?php at the start of the file:


error_reporting(-1);
ini_set('display_errors', 1);

They will cause any errors, warnings and notices to be displayed. For a live site you wouldn;t display any errors, warnings and notices, instead you would log them to an error file

There you are!
Thanks Drummin for the keen eye, this problem is solved now. I’m starting to wonder now what really the name attribute does.
But anyways, thanks a lot for the comment. This is what usually happens when you’re sleepy coding/studying.

Cheers,
Karl

Thanks for the tip bro.

I’m starting to wonder now what really the name attribute does

The name identifies the input field and will be the key in the $_POST array with its matching value. So <input name=“toy” value=“ball” /> becomes $_POST[‘toy’] would be ball.

Thanks for that clear explanation, I really learned from your comments.

Hi Drummin,
I am just wondering what’s the difference between an

id

and

name

attribute. I often notice that both attributes have the same value.
Thanks!

The name used in form inputs are the identifier that is sent on POST or GET that becomes the key as I already mentioned. The same name can be used more than on time as with radio buttons where a group might have the same name but only the selected radio value is sent to POST. An id on the other hand must be unique on a given page and is often used in Javascript where they want to reference an element by the id or style the element with css. The value given to an id is not sent to POST.

Thanks again for that Drummin, I understand it now clearly.
I have another question for you if you don’t mind, I am curious too with the fetch() and fetchAll(). Can you explain what’s the difference between the two?
Thanks for your patience and enlightenment.

Well I’m sure others could describe this better, but fetchall() is going to return the complete query result as an array that you can later loop through in some other way, e.g. using foreach()
The fetch() returns a single row of the fields defined in the query containing both the numeric array key and the column name for each field. Unless grabbing a single row of data, you will need to use WHILE to loop through each row. I tend to use FETCH_ASSOC as I just want column names in a WHILE loop. There may be times you just want FETCH_NUM for just using the numeric key. Just depends on what you’re wishing to do.