Dear Developers,

This is the part of my blog.While adding new post to my blog the category option not updating to database

add_post.php

    <h2>Add Post</h2>

    <?php

    //if form has been submitted process it
    if(isset($_POST['submit'])){

        $_POST = array_map( 'stripslashes', $_POST );

        //collect form data
        extract($_POST);

        //very basic validation
        if($postTitle ==''){
            $error[] = 'Please enter the title.';
        }

        if($postDesc ==''){
            $error[] = 'Please enter the description.';
        }

        if($postCont ==''){
            $error[] = 'Please enter the content.';
        }

        if(!isset($error)){

            try {

                $postSlug = slug($postTitle);

                //insert into database
                $stmt = $db->prepare('INSERT INTO blog_posts_seo (postTitle,postSlug,postDesc,postCont,postDate) VALUES (:postTitle, :postSlug, :postDesc, :postCont, :postDate)') ;
                $stmt->execute(array(
                    ':postTitle' => $postTitle,
                    ':postSlug' => $postSlug,
                    ':postDesc' => $postDesc,
                    ':postCont' => $postCont,
                    ':postDate' => date('Y-m-d H:i:s')
                ));
                $postID = $db->lastInsertId();

                //add categories
                if(is_array($catID)){
                    foreach($_POST['catID'] as $catID){
                        $stmt = $db->prepare('INSERT INTO blog_post_cats (postID,catID)VALUES(:postID,:catID)');
                        $stmt->execute(array(
                            ':postID' => $postID,
                            ':catID' => $catID
                        ));
                    }
                }

                //redirect to index page
                header('Location: index.php?action=added');
                exit;

            } catch(PDOException $e) {
                echo $e->getMessage();
            }

        }

    }

    //check for any errors
    if(isset($error)){
        foreach($error as $error){
            echo '<p class="error">'.$error.'</p>';
        }
    }
    ?>

    <form action='' method='post'>

        <p><label>Title</label><br />
        <input type='text' name='postTitle' value='<?php if(isset($error)){ echo $_POST['postTitle'];}?>'></p>

        <p><label>Description</label><br />
        <textarea name='postDesc' cols='60' rows='10'><?php if(isset($error)){ echo $_POST['postDesc'];}?></textarea></p>

        <p><label>Content</label><br />
        <textarea name='postCont' cols='60' rows='10'><?php if(isset($error)){ echo $_POST['postCont'];}?></textarea></p>

        <fieldset>
                <legend>Categories</legend>

    <?php    

    $stmt2 = $db->query('SELECT catID, catTitle FROM blog_cats ORDER BY catTitle');
    while($row2 = $stmt2->fetch()){

        if(isset($_POST['catID'])){

            if(in_array($row2['catID'], $_POST['catID'])){
              $checked = null;
            }else{
               $checked="checked='checked'"; 
            }
        }

        echo "<input type='checkbox' name='catID[]' value='".$row2['catID']."' $checked> ".$row2['catTitle']."<br />";
    }

    ?>

        </fieldset>

        <p><input type='submit' name='submit' value='Submit'></p>

    </form>

$catID is never given a value before line 44, so is_array will return false

@pritaeas :- Sir please have look this,

  $stmt->execute(array(
                            ':postID' => $postID,
                            ':catID' => $catID
                        ));

Line 44 above should be:

if (is_array($_POST['catID'])){

Not yet resolved, still have the same problem

Show your changed/latest code.

    <h2>Add Post</h2>

    <?php

    //if form has been submitted process it
    if(isset($_POST['submit'])){

        $_POST = array_map( 'stripslashes', $_POST );

        //collect form data
        extract($_POST);

        //very basic validation
        if($postTitle ==''){
            $error[] = 'Please enter the title.';
        }

        if($postDesc ==''){
            $error[] = 'Please enter the description.';
        }

        if($postCont ==''){
            $error[] = 'Please enter the content.';
        }

        if(!isset($error)){

            try {

                $postSlug = slug($postTitle);

                //insert into database
                $stmt = $db->prepare('INSERT INTO blog_posts_seo (postTitle,postSlug,postDesc,postCont,postDate) VALUES (:postTitle, :postSlug, :postDesc, :postCont, :postDate)') ;
                $stmt->execute(array(
                    ':postTitle' => $postTitle,
                    ':postSlug' => $postSlug,
                    ':postDesc' => $postDesc,
                    ':postCont' => $postCont,
                    ':postDate' => date('Y-m-d H:i:s')
                ));
                $postID = $db->lastInsertId();

                //add categories
                if (is_array($_POST['catID'])){
                    foreach($_POST['catID'] as $catID){
                        $stmt = $db->prepare('INSERT INTO blog_post_cats (postID,catID)VALUES(:postID,:catID)');
                        $stmt->execute(array(
                            ':postID' => $postID,
                            ':catID' => $catID
                        ));
                    }
                }

                //redirect to index page
                header('Location: index.php?action=added');
                exit;

            } catch(PDOException $e) {
                echo $e->getMessage();
            }

        }

    }

    //check for any errors
    if(isset($error)){
        foreach($error as $error){
            echo '<p class="error">'.$error.'</p>';
        }
    }
    ?>

    <form action='' method='post'>

        <p><label>Title</label><br />
        <input type='text' name='postTitle' value='<?php if(isset($error)){ echo $_POST['postTitle'];}?>'></p>

        <p><label>Description</label><br />
        <textarea name='postDesc' cols='60' rows='10'><?php if(isset($error)){ echo $_POST['postDesc'];}?></textarea></p>

        <p><label>Content</label><br />
        <textarea name='postCont' cols='60' rows='10'><?php if(isset($error)){ echo $_POST['postCont'];}?></textarea></p>

        <fieldset>
                <legend>Categories</legend>

    <?php    

    $stmt2 = $db->query('SELECT catID, catTitle FROM blog_cats ORDER BY catTitle');
    while($row2 = $stmt2->fetch()){

        if(isset($_POST['catID'])){

            if(in_array($row2['catID'], $_POST['catID'])){
              $checked = null;
            }else{
               $checked="checked='checked'"; 
            }
        }

        echo "<input type='checkbox' name='catID[]' value='".$row2['catID']."' $checked> ".$row2['catTitle']."<br />";
    }

    ?>

        </fieldset>

        <p><input type='submit' name='submit' value='Submit'></p>

    </form>

</div>

On Line 43 do:

print_r($_POST['catID']);

And post the result here. If it is not an array, then nothing happens. If there is no result at all, do:

print_r($_POST);

And post here.

Not effecting same result

<?php
    //if form has been submitted process it
    if(isset($_POST['submit'])){

        $_POST = array_map( 'stripslashes', $_POST );

        //collect form data
        extract($_POST);

        //very basic validation
        if($postTitle ==''){
            $error[] = 'Please enter the title.';
        }

        if($postDesc ==''){
            $error[] = 'Please enter the description.';
        }

        if($postCont ==''){
            $error[] = 'Please enter the content.';
        }

        if(!isset($error)){

            try {

                $postSlug = slug($postTitle);

                //insert into database
                $stmt = $db->prepare('INSERT INTO blog_posts_seo (postTitle,postSlug,postDesc,postCont,postDate) VALUES (:postTitle, :postSlug, :postDesc, :postCont, :postDate)') ;
                $stmt->execute(array(
                    ':postTitle' => $postTitle,
                    ':postSlug' => $postSlug,
                    ':postDesc' => $postDesc,
                    ':postCont' => $postCont,
                    ':postDate' => date('Y-m-d H:i:s')
                ));
                $postID = $db->lastInsertId();

                //add categories
                print_r($_POST['catID']);
                if (is_array($_POST['catID'])){
                    foreach($_POST['catID'] as $catID){
                        $stmt = $db->prepare('INSERT INTO blog_post_cats (postID,catID)VALUES(:postID,:catID)');
                        $stmt->execute(array(
                            ':postID' => $postID,
                            ':catID' => $catID
                        ));
                    }
                }

                //redirect to index page
                header('Location: index.php?action=added');
                exit;

            } catch(PDOException $e) {
                echo $e->getMessage();
            }

        }

    }

    //check for any errors
    if(isset($error)){
        foreach($error as $error){
            echo '<p class="error">'.$error.'</p>';
        }
    }
    ?>

    <form action='' method='post'>

        <p><label>Title</label><br />
        <input type='text' name='postTitle' value='<?php if(isset($error)){ echo $_POST['postTitle'];}?>'></p>

        <p><label>Description</label><br />
        <textarea name='postDesc' cols='60' rows='10'><?php if(isset($error)){ echo $_POST['postDesc'];}?></textarea></p>

        <p><label>Content</label><br />
        <textarea name='postCont' cols='60' rows='10'><?php if(isset($error)){ echo $_POST['postCont'];}?></textarea></p>

        <fieldset>
                <legend>Categories</legend>

    <?php    

    $stmt2 = $db->query('SELECT catID, catTitle FROM blog_cats ORDER BY catTitle');
    while($row2 = $stmt2->fetch()){

        if(isset($_POST['catID'])){

            if(in_array($row2['catID'], $_POST['catID'])){
              $checked = null;
            }else{
               $checked="checked='checked'"; 
            }
        }

        echo "<input type='checkbox' name='catID[]' value='".$row2['catID']."' $checked> ".$row2['catTitle']."<br />";
    }

    ?>

        </fieldset>

        <p><input type='submit' name='submit' value='Submit'></p>

    </form>

</div>

Second option also I am checked

print_r($_POST);

on same line but no error, no result
While aading a post to blog succefully updated but category option always blank

Post the HTML that is generated here.

Sir pls have look this html output

Member Avatar for diafol

That is not the html output, it's a screenshot. Copy/paste the HTML from "view source" in your browser.

Manual data entry

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Blog - Breaking News</title>
    <link rel="stylesheet" href="style/normalize.css">
    <link rel="stylesheet" href="style/main.css">
</head>
<body>

    <div id="wrapper">

        <h1>Blog</h1>
        <p>Posts in Breaking News</p>
        <hr />
        <p><a href="./">Blog Index</a></p>

        <div id='main'>

            <div><h1><a href="the-cyber-house-rules">The Cyber House Rules</a></h1><p>Posted on 6th Jun 2013 08:28:35 in <a href='c-weather'>Weather</a>, <a href='c-breaking-news'>Breaking News</a></p><p><p>You guys realize you live in a sewer, right? Uh, is the puppy mechanical in any way? Come, Comrade Bender! We must take to the streets! I daresay that Fry has discovered the smelliest object in the known universe! Good news, everyone! There's a report on TV with some very bad news!</p></p><p><a href="the-cyber-house-rules">Read More</a></p></div>
        </div>

        <div id='sidebar'>
            <h1>Recent Posts</h1>
<hr />

<ul>
<li><a href="test-for-cat">test for cat</a></li><li><a href="second-test">second test</a></li><li><a href="cat-test">Cat test</a></li><li><a href="test">test</a></li><li><a href="---">നിലമ്പൂർ</a></li></ul>

<h1>Catgories</h1>
<hr />

<ul>
<li><a href="c-environment">ENVIRONMENT</a></li><li><a href="c-health-news">Health News</a></li><li><a href="c-entertainment-news">Entertainment News </a></li><li><a href="c-business-news">Business News</a></li><li><a href="c-politics">Politics</a></li><li><a href="c-breaking-news">Breaking News</a></li><li><a href="c-sports">Sports</a></li><li><a href="c-weather">Weather</a></li></ul>

<h1>Archives</h1>
<hr />

<ul>
<li><a href='a-8-2016'>August</a></li><li><a href='a-6-2013'>June</a></li><li><a href='a-5-2013'>May</a></li></ul>      </div>

        <div id='clear'></div>

    </div>

</body>
</html>

While adding via online html form

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Blog - Cat test</title>
    <link rel="stylesheet" href="style/normalize.css">
    <link rel="stylesheet" href="style/main.css">
</head>
<body>

    <div id="wrapper">

        <h1>Blog</h1>
        <hr />
        <p><a href="./">Blog Index</a></p>

        <div id='main'>

            <div><h1>Cat test</h1><p>Posted on 11th Aug 2016 13:31:26 in </p><p><p>Cat testCat testCat testCat testCat testCat testCat testCat testCat testCat testCat testCat testCat testCat testCat testCat testCat testCat testCat testCat testCat testCat testCat testCat testCat testCat testCat testCat testCat testCat testCat testCat testCat testCat testCat testCat testCat testCat testCat testCat testCat testCat testCat testCat testCat test</p></p></div>
        </div>

        <div id='sidebar'>
            <h1>Recent Posts</h1>
<hr />

<ul>
<li><a href="test-for-cat">test for cat</a></li><li><a href="second-test">second test</a></li><li><a href="cat-test">Cat test</a></li><li><a href="test">test</a></li><li><a href="---">നിലമ്പൂർ</a></li></ul>

<h1>Catgories</h1>
<hr />

<ul>
<li><a href="c-environment">ENVIRONMENT</a></li><li><a href="c-health-news">Health News</a></li><li><a href="c-entertainment-news">Entertainment News </a></li><li><a href="c-business-news">Business News</a></li><li><a href="c-politics">Politics</a></li><li><a href="c-breaking-news">Breaking News</a></li><li><a href="c-sports">Sports</a></li><li><a href="c-weather">Weather</a></li></ul>

<h1>Archives</h1>
<hr />

<ul>
<li><a href='a-8-2016'>August</a></li><li><a href='a-6-2013'>June</a></li><li><a href='a-5-2013'>May</a></li></ul>      </div>

        <div id='clear'></div>

    </div>

</body>
</html>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.