...

View Full Version : Text won't show on the page.



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.

The reaper
06-22-2010, 08:30 AM
Just tried again and got a new message:

"Warning: Wrong parameter count for array_pop() in functions.inc.php on line 49"

I am so lost

xGIHavoc
06-22-2010, 09:04 AM
In functions.inc.php:
$sql = "SELECT title, entry
FROM entries
WHERE id=?
LIMIT 1"; and
$sql = "INSERT INTO entries (title, entry) VALUES (?, ?)";
In update.inc.php:
$sql = "INSERT INTO entries (title, entry) VALUES (?, ?)";

Those question marks are place holders for the real values to be inserted.

Do this:
<?php

$title = $_POST['title'];
$entry = $_POST['entry']; then replace the VALUES section of the INSERT INTO query with
VALUES ($title, $entry)";

It's quite simple, see if you can figure out the rest!

The reaper
06-22-2010, 09:31 AM
Thank you for pointed that out. But that hasn't changed the result any. Now, whenever I add a new post. I always had domain.com/?=0 which isn't good and nothing shows in the database. *holds lighter to the book*

xGIHavoc
06-22-2010, 10:18 AM
Check to see if the data is inserted properly now. There are quite a few pages you are leaving out, from what I can see.


$sql = "SELECT title, entry
FROM entries
WHERE id=?
LIMIT 1";

id=? should be id=$id by the way.

The reaper
06-22-2010, 04:31 PM
Hmmmm, I haven't left anything out that I know of. Those are all the page :confused:

If it's related in any way, If I put the data in manually through the "INSERT" tab in phpmyadmin, it displays if I go to the url. But it won't via the form.

The reaper
06-23-2010, 02:32 AM
Ok here is an update:

If I insert the text manually through phpmyadmin, it will display if I go to the url. If I leave the part in update.inc.php Where is says: VALUES (?, ?) it will save the entry into the database but it will save it as NULL ( I don't know what the means) but if I change it to the way you said to, it doesn't save anything.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum