Publishing MySQL Data on the Web Insert not working

Hello, I purchased the book PHP and MySQL Novice to Ninja Fifth edition and am trying the exercise that inserts a new joke from a form input thus adding a new entry into my database. Everything seems to work except the insert portion. The index.php is as follows:

<?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=ijdb', 'ijdbuser',
	'mypassword');
	$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();
}
try
{
	$sql = 'SELECT joketext FROM joke';
	$result = $pdo->query($sql);
}
catch (PEOException $e)
{
	$error = 'Error fetching jokes: ' . $e->getMessage();
	include 'error.html.php';
	exit();
}

while ($row = $result->fetch())
{
	$jokes[] = $row['joketext'];
}

include 'jokes.html.php';

It looks like everything is in there just as the book suggested. I can see that the database connection is good because the query pulls, however when I click on the add button to submit the form nothing gets added.
Here is the form code:

<!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>

And finally it launches the form from the following page which has the output with the link to the form as follows:

<!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 ($jokes as $joke): ?>
		<blockquote>
			<p><?php echo htmlspecialchars($joke, ENT_QUOTES, 'UTF-8');
				?>
			</p>
		</blockquote>
	<?php endforeach; ?>
</body>
</html>

Can anyone help with this? What am I doing wrong? I followed what the book said to do.

If you get no error but no insert has been done then probably the if statement is never entered. Try a var_dump of $_POST to check the content

Missing anything here???
Maybe =

2 Likes

Newbie mistake. Works like a charm. Thank you.

Good one. I missed it :blush:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.