Why is this index undefined

I have this program it uses two files. One is the handler and the other is the actual page.

Here is the handler:
[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);
}
if (isset($_GET[‘addjoke’]))
{
include ‘form.html.php’;
exit();
}
try
{
$pdo = new PDO(‘mysql:host=localhost;dbname=lphp’, ‘root’,
‘’);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->exec(‘SET NAMES “utf8”’);
}
catch (PDOException $e)
{
$error = ‘Unable to connect to the database server.’;
include ‘error.html.php’;
exit();
}
if (isset($_POST[‘joketext’]))
{
try
{
$sql = ‘INSERT INTO joke SET
joketext = joketext,
jokedate = CURDATE()’;
$s = $pdo->prepare($sql);
$s->bindValue(’:joketext’, $_POST[‘joketext’]);
$s->execute();
}
catch (PDOException $e)
{
$error = ‘Error adding submitted joke: ’ . $e->getMessage();
include ‘error.html.php’;
exit();
}
header(‘Location: .’);
exit();
}
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.html.php’;
exit();
}
header(‘Location: .’);
exit();
}
try
{
$sql = ‘SELECT joke.id, LEFT(joketext, 20), name, email FROM joke INNER JOIN author ON authorid = author.id’;
$result = $pdo->query($sql);
}
catch (PDOException $e)
{
$error = 'Error fetching jokes: ’ . $e->getMessage();
include ‘error.html.php’;
exit();
}
$jokes = array();
while ($row = $result->fetch())
{
$jokes[] = array(‘id’ => $row[‘id’], ‘text’ => $row[‘joketext’], ‘name’ => $row[‘name’], ‘email’ => $row[‘email’]);
}
include ‘jokes.html.php’;
[/php]

and here is the actual page:
[php]

List of Jokes

Add your own joke

Here are all the jokes in the database:

<?php foreach ($jokes as $joke): ?>

<?php echo htmlspecialchars($joke['joketext'], ENT_QUOTES, 'UTF-8'); ?> (by <?php echo htmlspecialchars($joke['name'], ENT_QUOTES, 'UTF-8'); ?>)

<?php endforeach; ?> [/php]

I get the following notice on the page and I would really like to know why this index is undefined please:
Undefined index: joketext in C:\wamp\www\Netbeans\PHP Power\index.php on line 93

Any help is greatly appreciated

and now the jokes are not being displayed. They are in the database!

All you need to do is look at the array you set it to be at the bottom of the first block of code you posted:
[php]$jokes[] = array(‘id’ => $row[‘id’], ‘text’ => $row[‘joketext’], ‘name’ => $row[‘name’], ‘email’ => $row[‘email’]);[/php]

You are trying to access the key “joketext” in the array above. If you are unable to work it out yourself from this information, let me know - but I believe in helping people to work the problem out for themselves so as to better ingrain the problem’s solution.

What comes up when you print_r $joke?

@Smokey PHP

That is about the best response I have ever had to any issue I posted on any forum.

I looked and figured it out that the key should be ‘text’ and not ‘joketext’ and so now it is working like it should.

Thank you this way I will learn PHP faster and get some good exercise for my brain. Your help is greatly appreciated

Glad to be of help! Any further questions just make another topic and me or one of the other guys here should be able to sort you out. Happy coding!

Sponsor our Newsletter | Privacy Policy | Terms of Service