...

View Full Version : Resolved new blog post only works sometimes



firetag
08-30-2009, 02:11 AM
Fixed it.


I have been working on this mini blog and the function I created for adding new blog posts to the database
doesn't seem to be working. It only worked once so I have no idea what's wrong with it..
Also what else should I include in my code for security?

Here is the code for the blogpost class:


class blogpost{

public $id;
public $title;
public $body;
public $datePosted;

function __construct($inId = null,$inTitle = null, $inBody = null, $inDatePosted = null)
{
if(!empty($inid))
{
$this->id=$inId;
}
if(!empty($inTitle))
{
$this->title=$inTitle;
}
if(!empty($inBody))
{
$this->body=$inBody;
}
if(!empty($inDatePosted))
{
$splitDate=explode("-",$inDatePosted);
$this->datePosted=$splitDate[1] . "/" . $splitDate[2] . "/" . $splitDate[0];
}
}

public static function getBlogPosts ($inId = null)
{

$query = mysql_query("SELECT * FROM blog_posts ORDER BY id DESC");


$postArray = array();
$x=1;
while($row = mysql_fetch_assoc($query))
{
$myPost = new blogpost($row["id"],$row["title"], $row["body"], $row["date_posted"]);
$postArray[$x]=$myPost;
$x++;
}
return $postArray;
}
public static function makeBlogPost ($inTitle, $inBody)
{

$inTitle=mysql_real_escape_string($inTitle);
$inBody=mysql_real_escape_string($inBody);
mysql_query("INSERT INTO blog_posts(title,body) VALUES ('".$inTitle."' , '".$inBody."')"); // returns true upon success
}
}

Here is the code for index (displaying the forms):


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<?php
include("conn.php");
include("classes.php");
$blogPosts = blogpost::getBlogPosts();
?>
</head>

<body>
<?php
foreach($blogPosts as $row){?>




<h2><?php echo $row ->title; ?></h2>
<h5><?php echo $row->datePosted; ?></h5>
<p><?php echo $row->body; ?></p>
<hr />
<?php } ?>
<form name="newBlogPost" action="newpost.php" method="post">
<label for="title">Title:&nbsp;</label>
<input name="title" type="text" />
<label for="body">Body:&nbsp;</label>
<input name="body" type="text" />
<input type="submit" value="Submit" />
</form>
</body>
</html>

And here is the code for newpost.php (which handles the form data):

Edit: I think it may have something to do with the header redirect in the code below... Any thoughts?

include("classes.php");

$title=$_POST["title"];
$body=$_POST["body"];
blogpost::makeBlogPost($title, $body);

header('Location: http://localhost/blog/index.php');

kbluhm
08-30-2009, 04:15 AM
You say it only worked once. Is it possible you have a primary key that is not set to `auto_increment`?

Also check this bit of code:



function __construct($inId = null,$inTitle = null, $inBody = null, $inDatePosted = null)
{
if(!empty($inid))
{
$this->id=$inId;
}

The parameter is $inId, but you checking whether $inid is empty();

firetag
08-30-2009, 12:56 PM
It is set to auto increment and I fixed the $inId typo, but it still doesn't work I think it has something to do with my header redirect I am going to redo it with meta tags to see if that works, but thanks for the reply.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum