I am trying to learn some PHP using the book titled PHP for Absolute Beginners. I am trying to implement some blog design code given in the book using WAMP and editplus. When I try to insert the data using PHP form, all I get is NULL values in the database table. Here is a piece of code for inserting values into the DB.
<?php
if($_SERVER['REQUEST_METHOD']=='POST'
&& $_POST['submit']=='Save Entry'
&& !empty($_POST['title'])
&& !empty($_POST['entry']))
{
// Include database credentials and connect to the database
include_once 'db.inc.php';
$db = new PDO(DB_INFO, DB_USER, DB_PASS);
// Save the entry into the database
$sql = "INSERT INTO entries (title, entry) VALUES (?, ?)";
$stmt = $db->prepare($sql);
$stmt->execute(array($title, $entry));
$stmt->closeCursor();
// Get the ID of the entry we just saved
$id_obj = $db->query("SELECT LAST_INSERT_ID()");
$id = $id_obj->fetch();
$id_obj->closeCursor();
// Send the user to the new entry
header('Location: ../admin.php?id='.$id[0]);
exit;
}
// If both conditions aren't met, sends the user back to the main page
else
{
header('Location: ../admin.php');
exit;
}
?>
When I check the apache error log I see this:
[Sun May 27 19:21:24 2012] [error] [client 127.0.0.1] PHP Notice: Undefined
variable: title in C:\\wamp\\www\\examples\\simple_blog\\inc\\update.inc.php
on line 14, referer: http://localhost/examples/simple_blog/admin.php?id=8
[Sun May 27 19:21:24 2012] [error] [client 127.0.0.1] PHP Stack trace:, referer:
http://localhost/examples/simple_blog/admin.php?id=8
[Sun May 27 19:21:24 2012] [error] [client 127.0.0.1] PHP 1. {main}()
C:\\wamp\\www\\examples\\simple_blog\\inc\\update.inc.php:0, referer:
http://localhost/examples/simple_blog/admin.php?id=8
[Sun May 27 19:21:24 2012] [error] [client 127.0.0.1] PHP Notice: Undefined
variable: entry in C:\\wamp\\www\\examples\\simple_blog\\inc\\update.inc.php
on line 14, referer: http://localhost/examples/simple_blog/admin.php?id=8
[Sun May 27 19:21:24 2012] [error] [client 127.0.0.1] PHP Stack trace:, referer:
http://localhost/examples/simple_blog/admin.php?id=8
[Sun May 27 19:21:24 2012] [error] [client 127.0.0.1] PHP 1. {main}()
C:\\wamp\\www\\examples\\simple_blog\\inc\\update.inc.php:0, referer:
http://localhost/examples/simple_blog/admin.php?id=8
I don't know what these errors are. Please help me.
$titledoes not exist, but$_POST['title']does. You are already using the correct variables in your if conditional, so why not when executing the query?$entry. I hope this book wasn't written withregister_globalsturned on. If it's that old, you should probably get a new book.PDOat least :)