0

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.

3
  • 1
    You are using variables that you have not defined. $title does not exist, but $_POST['title'] does. You are already using the correct variables in your if conditional, so why not when executing the query? Commented May 27, 2012 at 14:16
  • 1
    You haven't defined $entry. I hope this book wasn't written with register_globals turned on. If it's that old, you should probably get a new book. Commented May 27, 2012 at 14:16
  • 1
    @minitech they do mention PDO at least :) Commented May 27, 2012 at 14:18

3 Answers 3

1

Possibly your server is configured so that $_POST['title'] is not automatically aliased as $title. Initialize $title = $_POST['title'] and same for other used $_POST items manually.

<?php
if($_SERVER['REQUEST_METHOD']=='POST'
&& $_POST['submit']=='Save Entry'
&& !empty($_POST['title'])
&& !empty($_POST['entry']))
{
    $title = $_POST['title'];
    $entry = $_POST['entry'];

...
}
Sign up to request clarification or add additional context in comments.

5 Comments

Why would $_POST['title'] ever be aliased as $title automatically?
@Flukey: PHP has (had?) an option register_globals, which made all variables from POST, GET and COOKIE available as global variables.
It it not enabled by default and with good reason. This feature has been DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 5.4.0.
Yes, but it seems, that code provided in question assumes that register_globals is on.
I think this book is a bit old so it might have been configured as register_globals as ON, so there was no need to declare the variables.
0

PHP Notice: Undefined variable: title means that there isn't a variable called title. And looking at your code, you're almost there, but have just missed out two lines. YOu need to assign your $_POST data the variable. Add the follow lines just inside the if statement and you should be ok

$title=$_POST['title'];
$entry=$_POST['entry'];

Comments

0

The errors/warnings are pretty self explanatory; you must define variables before you can use them (most of the times):

$title = $_POST['title'];
$entry = $_POST['entry'];

Anywhere above line 14 will do.

PHP has a register_globals setting that would do this automatically for you, but because it's a configurations setting, you can't reliably use it. I believe that in the latest version of PHP it's no longer available.

2 Comments

I thought $_POST['title']; will automatically get defined as $title so I never explicitly defined it. Thanks for your help.
@omsai some php configurations allow for this, but it's deprecated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.