I am stuck in a chap 5 for a while now! i followed all the examples in the book to design a simple blog application in PHP but in chapter 5 i am not able to retrieve entries and title. I checked if the data was being stored in the database and not getting displayed , but it’s not even getting stored in the data base.
here’s the index.php file code
<?php
include_once ‘inc/functions.inc.php’;
include_once ‘inc/db.inc.php’;
$db = new PDO(DB_INFO,DB_USER,DB_PASS);
$id= (isset($_GET[‘id’])) ? (int) $_GET[‘id’] :NULL;
$e=retrieveEntries($db,$id);
// Get the fulldisp flag and remove it from the array
$fulldisp = array_pop($e);
// Sanitize the entry data
$e = sanitizeData($e);
?>
<!DOCTYPE html
PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
<html xmlns=“http://www.w3.org/1999/xhtml” xml:lang=“en” lang=“en”>
<head>
<meta http-equiv=“Content-Type”
content=“text/html;charset=utf-8” />
<link rel=“stylesheet” href=“/css/default.css” type=“text/css” />
<title> Simple Blog </title>
</head>
<body>
<h1> Simple Blog Application </h1>
<div id=“entries”>
<?php
if($fulldisp==1)
{
?>
<h2> <?php echo $e[‘title’] ?> </h2>
<p> <?php echo $e[‘entries’] ?> </p>
<p class=“backlink”>
<a href=“./”>Back to the Latest Entries</a>
</p>
<?php
}
else
{
foreach($e as $entry) {
?>
<p>
<a href=“?id=<?php echo $entry[‘id’]?>”>
<?php echo $entry[‘title’] ?>
</a>
</p>
<?php
}
}
?>
<p class=“backlink”>
<a href=“admin.php”>Post A New Entry!</a>
</p>
</div>
here’s the functions.inc.php code
<?php
function retrieveEntries ($db,$id=NULL)
{
if(isset($id))
{
$sql= “SELECT title,entry
FROM entries
WHERE id=?
LIMIT 1”;
$stmt=$db->prepare($sql);
$stmt->execute(array($_GET[‘id’]));
$e=$stmt->fetch();
$fulldisp=1;
}
else
{
$sql= “SELECT id, title
FROM entries
ORDER BY created DESC”;
foreach($db->query($sql) as $row) {
$e= array(
‘id’=> $row[‘id’],
‘title’=> $row[‘title’]
);
}
$fulldisp=0;
if(!is_array($e))
{
$fulldisp=1;
$e= array(
‘title’=> ‘No Entries Yet’,
‘entry’=>‘<a href=“/admin.php”>Post an entry!</a>’
);
}
}
array_push($e, $fulldisp);
return $e;
}
function sanitizeData($data)
{
if(!is_array($data))
{
return strip_tags($data, “<a>”);
}
else
{
return array_map(‘sanitizeData’, $data);
}
}
?>
and here’s the update.inc.php code
<?php
if($_SERVER[‘REQUEST_METHOD’]==‘POST’
&& $_POST[‘submit’]==‘Save Entry’)
{
include_once ‘db.inc.php’;
$db=new PDO(DB_INFO, DB_USER, DB_PASS);
$sql= “INSERT INTO entries (title,entry) VALUES (?,?)”;
$stmt= $db->prepare($sql);
$stmt-> execute(array($_POST[‘title’],$_POST[‘entry’]));
$stmt->closeCursor();
$id_obj= $db->query(“SELECT LAST_INSERT_ID()”);
$id= $id_obj->fetch();
$id_obj->closeCursor();
header(‘Location: …/?id=’.$id[0]);
exit;
}
else
{
header(‘Location: …/’);
exit;
}
?>