0

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>

Edited by pritaeas: Fixed markdown.

3
Contributors
14
Replies
47
Views
1 Year
Discussion Span
Last Post by Faisal_13
Featured Replies
0

@pritaeas :- Sir please have look this,

  $stmt->execute(array(
                            ':postID' => $postID,
                            ':catID' => $catID
                        ));
0
    <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>
0

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.

0

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>
0

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

0

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

0

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>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.