PHP not adding joke to database

Ho. I’m going through the PHP and MySQL Novice to Ninja book, and doing the add joke part.
As far as I can see, th ecode is right. I’m not getting any errors, but when it redirects to the joke list, the new jokes don’t show up/ I’m not getting any errors. Checked the MySQL database at my site and the new jokes aren’t being added.

Here is my code:

//index.php

<?php
if (get_magic_quotes_gpc())
{
$process = array(&$GET, &$_COOKIE, &$_REQUEST);
while (list($key, $val) = each($process))
{
foreach ($val as $k=> $v)
{
	unset($process[$key][$k]);
	if (is_array($v))
	{
	$process[$key][stripeslashes($k)]=$v;
	$process[] = &$process[$key][stripeslashes($k)];
	}
	else
	{
	$process[$key][stripeslashes($k)] = stripeslashes($v);
		
}
}
}
unset($process);
}
	if (isset($_GET['addjoke']))
	{
	include 'form.html.php';
	exit();
	}
	try
	{
	$pdo = new PDO('mysql:host=localhost;dbname=greyto5_ijdb','********', '*******');
	$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 jokes 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();
				}
				try
				{
				$sql ='SELECT joketext FROM jokes';
				$result = $pdo->query($sql);
				}
				catch(PDOException $e)
				{
				$error = 'Error fetching jokes: '. $e->getMessage();
				include 'error.html.php';
				exit();
				}
				while($row = $result->fetch())
				{
				$jokesarray[] = $row['joketext'];
				}
				include 'jokes.html.php';

////jokes.html.php

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>List of Jokes</title>
</head>
<body>  
<p><a href="?addjoke"> Add your own joke</a></p>
<p>Here are all the jokes in the database:</p>
<?php foreach ($jokesarray as $joke): ?>
<blockquote>
<p> <?php echo htmlspecialchars($joke, ENT_QUOTES, 'UTF-8');?>
</p>
</blockquotes>
<?php endforeach; ?>
</body>
</html>

//form.html.php

<!DOCTYPE html>
<html lang="en"
<head> 
<meta charset="utf-8">
<title>Add Joke</title>
<style type="text/css"> 
textarea {
	display: block;
	width: 100%;
	}
	</style>
	</head>
	<body>
	<form action ="?" method = "post">
	<div>
	<label for ="joketext">Type your joke here:</label>
	<textarea id="joketext" name="joketext" rows="3" cols="40">
	</textarea>
	</div>
	<div><input type="submit" value="Add"></div>
	</form>
	</body>
	</html>

///error.html.php

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Script Error</title>
  </head>
  <body>
    <p>
      <?php echo $error; ?>
    </p>
  </body>
</html>

umm first of all, there are typos in your code, it should be $_GET, not $GET. Similarly the if condition should check $_POST[‘joketext’], not $POST[‘joketext’], this may be the cause of your problem but it may be more.

Second, shouldnt the last variable in the array be $_POST rather than $_REQUEST? Note the superglobal array request contains get, post and cookie data, so you are essentially duplicating things as whatever is in $_GET is also in $_REQUEST.

Third, why are you passing reference of the superglobals to an array? Superglobals are available everywhere, if you modify them the changes will appear in all parts of your script. You only use pass by reference when working on integers, strings or other PHP primitive types, but if you have good reasons behind this pass by reference behavior Id be glad to hear.

Also magic quotes are deprecated and removed in the latest version, avoid them unless you have to.

Thanks. I’ll change and check when I get home.

Shouldn’t it be stripslashes() in that first loop instead of stripeslashes, are they typos? Though I would imagine that would cause an error message.

Thanks for your help. It was a syntax error with the post like you said. I changed it and it worked. I’ll use an IDE when something crazy happens from now on.

Try this at the top of your Php files:


<?php 
error_reporting(-1);
ini_set('display_errs', true);


It will show all syntax, undeclared variables, etc

will do.

meant for this to be in the other post.