I’m studying BYO database driven website by kevin yank pg 158-162 “Select with multiple tables” section. I have simply copied everything according to the book and when I run the page it’s giving me an error saying; “Undefined variable error…”. When I check the path to the line where the problem is i still feel the variable has been defined… I’m stuck! code below for both the Dom & php script:
//…THE DOM
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
<html xmlns=“http://www.w3.org/1999/xhtml”>
<head>
<title>List/delete of jokes</title>
<meta http-equiv=“content-type” content=
“text/html; charset=utf-8” />
</head>
<body>
<p><a href=“?addjoke”>Add you own joke</a></p>
<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="mailto:<?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>
//…PHP SCRIPT
<?php
if (get_magic_quotes_gpc()) {
function stripslashes_deep($value) {
$value = is_array($value) ?
array_map(‘stripslashes_deep’, $value) :
stripslashes($value);
return $value;
}
$_POST = array_map('stripslashes_deep', $_POST);
$_GET = array_map('stripslashes_deep', $_GET);
$_COOKIE = array_map('stripslashes_deep', $_COOKIE);
$_REQUEST = array_map('stripslashes_deep', $_REQUEST);
}
if (isset($_GET['addjoke'])) {
include 'form.html.php';
exit();
}
// establishing connection with database
$link = mysqli_connect(‘localhost’, ‘root’);
if (!$link) {
$error = ‘Unable to connect to the server.’;
include ‘error.html.php’;
exit();
}
if (!mysqli_set_charset($link, ‘utf8’)) {
$output = ‘Unable to set database connection encoding.’;
include ‘output.html.php’;
exit();
}
if (!mysqli_select_db($link, ‘ijdb’)) {
$error = ‘Unable to locate the Joke database.’;
include ‘error.html.php’;
exit();
}
if (isset($_POST[‘joketext’])) {
$joketext = mysqli_real_escape_string($link, $_POST[‘joketext’]);
$sql = ‘INSERT INTO joke SET
joketext = "’ . $joketext . ‘",
jokedate = CURDATE()’;
if (!mysqli_query($link, $sql)) {
$error = 'Error adding submitted joke: ’ . mysqli_error($link);
include ‘error.html.php’;
exit();
}
header(‘Location: .’);
exit();
}
$result = mysqli_query($link, ‘SELECT joke.id, joketext, name, email
FROM joke INNER JOIN author ON authorid = author.id’);
if (!$result) {
$error = 'Error fetching jokes: ’ . mysqli_error($link);
include ‘error.html.php’;
exit();
}
while ($row = mysqli_fetch_array($result)) {
$jokes[] = array(‘id’ => $row[‘id’], ‘text’ => $row[‘joketext’],
‘name’ => $row[‘name’], ‘email’ => $row[‘email’]);
}
include ‘jokes.html.php’;
?>
//this is the error
[COLOR=“Red”]Notice: Undefined variable: jokes in C:\wamp\www\sitepoint\jokes\jokes.html.php on line 12
Warning: Invalid argument supplied for foreach() in C:\wamp\www\sitepoint\jokes\jokes.html.php on line 12[/COLOR]