The reaper
06-22-2010, 07:59 AM
Hello there.
I have been building a simple blog system out of a book name PHP: For Absolute Beginners and the name is exactly right. Well, I have all the pages done, and it works mostly. When I save the new post, and navigate to the webpage, it doesn't show. Just the back link (refer to the link at the bottom.
Here are all the associated pages. Please look over this. If you can find the bug, would you explain why it isn't working?
index.php
<?php include("tophead.php"); ?>
<?php include("header.php"); ?>
<?php
include_once 'admin/inc/functions.inc.php';
include_once 'admin/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);
$fulldisp = array_pop($e);
$e = sanitizeData($e);
?>
<br />
<div id="entries">
<?php
if($fulldisp==1)
{
?>
<h2> <?php echo $e['title'] ?> </h2>
<p> <?php echo $e['entry'] ?> </p>
<p class="backlink">
<a href="./">Back to Latest Entries</a>
</p>
<?php
}
?>
</div>
</body>
</html>
functions.inc.php
<?php
function retrieveEntries($db, $id=NULL)
{
// Get entries from database
if(isset($id))
{
// Load specified entry
$sql = "SELECT title, entry
FROM entries
WHERE id=?
LIMIT 1";
$stmt = $db->prepare($sql);
$stmt->execute(array($_GET['id']));
// Save the returned entry array
$e = $stmt->fetch();
// Set the fulldisp flag for a single entry
$fulldisp = 1;
}
else
{
// Load all entry titles
$sql = "SELECT id, title
FROM entries
ORDER BY created DESC";
foreach($db->query($sql) as $row) {
$e[] = array(
'id' => $row['id'],
'title' => $row['title']
);
}
// Set the fulldisp flag for multiple entries
$fulldisp = 0;
if(!is_array($e))
{
$fulldisp = 1;
$e = array(
'title' => 'No Entries Yet',
'entry' => '<a href="/admin.php">Post an entry!</a>'
);
}
}
// Return loaded data
array_push($e, $fulldisp);
return $e;
}
function sanitizeData($data)
{
if(!is_array($data))
{
return strip_tags($data, "<a>");
}
else
{
return array_map('sanitizeData', $data);
}
}
?>
update.inc.php
<?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: ../adminwrite.php?id='.$id[0]);
exit;
// Continue processing information . . .
}
// If both conditions aren't met, sends the user back to the main page
else
{
header('Location: ../adminwrite.php');
exit;
}
?>
Link to database pic http://newestfunny.com/db.jpg
The only thing I can think of that is wrong is it is saving the text in the database incorrectly. But I have no idea.
Again, thank you a bunch! As you can see on my clock, it is very late.
I have been building a simple blog system out of a book name PHP: For Absolute Beginners and the name is exactly right. Well, I have all the pages done, and it works mostly. When I save the new post, and navigate to the webpage, it doesn't show. Just the back link (refer to the link at the bottom.
Here are all the associated pages. Please look over this. If you can find the bug, would you explain why it isn't working?
index.php
<?php include("tophead.php"); ?>
<?php include("header.php"); ?>
<?php
include_once 'admin/inc/functions.inc.php';
include_once 'admin/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);
$fulldisp = array_pop($e);
$e = sanitizeData($e);
?>
<br />
<div id="entries">
<?php
if($fulldisp==1)
{
?>
<h2> <?php echo $e['title'] ?> </h2>
<p> <?php echo $e['entry'] ?> </p>
<p class="backlink">
<a href="./">Back to Latest Entries</a>
</p>
<?php
}
?>
</div>
</body>
</html>
functions.inc.php
<?php
function retrieveEntries($db, $id=NULL)
{
// Get entries from database
if(isset($id))
{
// Load specified entry
$sql = "SELECT title, entry
FROM entries
WHERE id=?
LIMIT 1";
$stmt = $db->prepare($sql);
$stmt->execute(array($_GET['id']));
// Save the returned entry array
$e = $stmt->fetch();
// Set the fulldisp flag for a single entry
$fulldisp = 1;
}
else
{
// Load all entry titles
$sql = "SELECT id, title
FROM entries
ORDER BY created DESC";
foreach($db->query($sql) as $row) {
$e[] = array(
'id' => $row['id'],
'title' => $row['title']
);
}
// Set the fulldisp flag for multiple entries
$fulldisp = 0;
if(!is_array($e))
{
$fulldisp = 1;
$e = array(
'title' => 'No Entries Yet',
'entry' => '<a href="/admin.php">Post an entry!</a>'
);
}
}
// Return loaded data
array_push($e, $fulldisp);
return $e;
}
function sanitizeData($data)
{
if(!is_array($data))
{
return strip_tags($data, "<a>");
}
else
{
return array_map('sanitizeData', $data);
}
}
?>
update.inc.php
<?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: ../adminwrite.php?id='.$id[0]);
exit;
// Continue processing information . . .
}
// If both conditions aren't met, sends the user back to the main page
else
{
header('Location: ../adminwrite.php');
exit;
}
?>
Link to database pic http://newestfunny.com/db.jpg
The only thing I can think of that is wrong is it is saving the text in the database incorrectly. But I have no idea.
Again, thank you a bunch! As you can see on my clock, it is very late.