Delete rows from the database

Hi,

Good day to all!
I found this site through Kevin Yank’s book and I’m looking forward to maximize this forum as much as possible.
I’m totally new to PHP and I’m reading PHP & MySQL: Novice to Ninja 5th Edition by Kevin Yank

I’ve got a problem in Chapter 4: Delete joke.
Everything seems okay, the code is okay but I’m getting this error.

Please see my code below.

if (isset($_GET['deletejoke'])) {
		try {
			$sql = 'DELETE FROM joke WHERE id = :id';
			$s = $pdo->prepare($sql);
			$s->bindValue(':id', $_POST['id']);
			$s->execute();
		} catch (PDOException $e) {
			$error = 'Error deleting joke' . $e->getMessage();
			include 'error.php';
			exit();
		}
			header('Location: .');
			exit();
	}

	try {
		$sql = 'SELECT id, joketext FROM joke';
		$result = $pdo->query($sql);
	} catch (PDOException $e) {
		$error = 'Error fetching jokes' . $e->getMessage();
		include 'error.php';
		exit();
	}
		foreach ($result as $row) {
			$jokes = array('id' => $row['id'], 'text' => $row['joketext']);
		}
		include 'jokes.php';
	?>
<!DOCTYPE html>
<html lang="en">
<head>
	<title>Exercise #3: Deleting jokes</title>
	<meta charset="utf-8"/>
</head>
<body>
	<a href="?addjoke">Add your own joke!</a>
	<p>Here are all the jokes in the database:</p>
	<?php foreach($jokes as $joke): ?>
	<form action="?deletejoke" method="post">
		<blockquote>
			<p>
				<?php echo htmlspecialchars($joke['text'], ENT_QUOTES, 'UTF-8'); ?>
				<input type="hidden" name="id" value="<?php echo $joke['id']; ?>">
				<input type="submit" value="Delete">
				(by <a href="mail to:<?php echo htmlspecialchars($joke['email'], ENT_QUOTES, "UTF-8"); ?>">
				<?php echo htmlspecialchars($joke['name'], ENT_QUOTES, "UTF-8");?></a>)
			</p>
		</blockquote>
	</form>		
	<?php endforeach; ?>	
</body>
</html>

Any feedbacks are highly appreciated.

Thank,
Karl

I believe that error is coming from this section.

foreach ($result as $row) {
            $jokes = array('id' => $row['id'], 'text' => $row['joketext']);
        }

Try changing to this.

		$jokes = array();
	$i=0;
	foreach ($result as $row) {
		$jokes[$i]['id'] = $row['id'];
		$jokes[$i]['text'] = $row['joketext'];
		$i++;
	}

Sir thank you for your reply. I already figured out the culprit. It’s just some typo.
Instead of

foreach ($result as $row) {
            $jokes = array('id' => $row['id'], 'text' => $row['joketext']);
        }

The correct one is

foreach ($result as $row) {
            $jokes[] = array('id' => $row['id'], 'text' => $row['joketext']);
        }