Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jul 2009
    Posts
    52
    Thanks
    6
    Thanked 1 Time in 1 Post

    new blog post only works sometimes

    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:

    PHP Code:
    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):

    Code:
    <!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?
    PHP Code:
    include("classes.php");

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

    header('Location: http://localhost/blog/index.php'); 
    Last edited by firetag; 08-30-2009 at 01:50 PM.

  • #2
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    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:
    PHP 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();
    Last edited by kbluhm; 08-30-2009 at 04:59 AM.

  • Users who have thanked kbluhm for this post:

    firetag (08-30-2009)

  • #3
    New Coder
    Join Date
    Jul 2009
    Posts
    52
    Thanks
    6
    Thanked 1 Time in 1 Post
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •