简体   繁体   English

PHP记录未插入MySql Blog类别表

[英]PHP Record Not Inserting into MySql Blog Categories Table

I have an addpost.php which has a form that submits a blog article into a mysql database. 我有一个addpost.php,它具有将博客文章提交到mysql数据库的形式。 The tables are: 这些表是:

blog_post_cats blog_post_cats

+--------+------------------+------+-----+---------+----------------+
| Field  | Type             | Null | Key | Default | Extra          |
+--------+------------------+------+-----+---------+----------------+
| id     | int(11) unsigned | NO   | PRI | NULL    | auto_increment |
| postID | int(11)          | YES  |     | NULL    |                |
| catID  | int(11)          | YES  |     | NULL    |                |
+--------+------------------+------+-----+---------+----------------+

blog_posts_seo blog_posts_seo

+-----------+------------------+------+-----+---------+----------------+
| Field     | Type             | Null | Key | Default | Extra          |
+-----------+------------------+------+-----+---------+----------------+
| postID    | int(11) unsigned | NO   | PRI | NULL    | auto_increment |
| postTitle | varchar(255)     | YES  |     | NULL    |                |
| postDesc  | text             | YES  |     | NULL    |                |
| postCont  | text             | YES  |     | NULL    |                |
| postDate  | datetime         | YES  |     | NULL    |                |
+-----------+------------------+------+-----+---------+----------------+

blog_cats blog_cats

+----------+------------------+------+-----+---------+----------------+
| Field    | Type             | Null | Key | Default | Extra          |
+----------+------------------+------+-----+---------+----------------+
| catID    | int(11) unsigned | NO   | PRI | NULL    | auto_increment |
| catTitle | varchar(255)     | YES  |     | NULL    |                |
+----------+------------------+------+-----+---------+----------------+

When a user submits the form, the post title, content, description gets posted to the blog_posts_seo table, but also the category selected passes the catID and postID to the blog_post_cats table. 当用户提交表单时,帖子标题,内容,描述将发布到blog_posts_seo表中,而且所选类别还将catID和postID传递到blog_post_cats表中。 Below is an example of the form fields. 以下是表单字段的示例。 Also how the category names and checkboxes get displayed from querying the blog_cats table: 以及如何通过查询blog_cats表显示类别名称和复选框:

<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="checked='checked' ";
                }else{
                   $checked = null;
                }
            }

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

        ?>

    </fieldset>

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

</form>

So if the submit button is pressed, the insert statements get executed: 因此,如果按下了提交按钮,则执行insert语句:

try {


                //insert into database
                $stmt = $db->prepare('INSERT INTO blog_posts_seo (postTitle,postDesc,postCont,postDate) VALUES (:postTitle, :postDesc, :postCont, :postDate)') ;
                $stmt->execute(array(
                    ':postTitle' => $postTitle,
                    ':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;

What is actually happening is NOTHING is getting passed to the blog_post_cats table!! 实际发生的是没有什么传递给blog_post_cats表! So I am not sure what is going wrong? 所以我不确定出什么问题了吗?

I think the insert statement is correct for inserting the catID and postID to the blog_post_cats table, so I am suspecting there is a mistake with the following: 我认为insert语句对于将catID和postID插入blog_post_cats表是正确的,因此我怀疑以下内容有误:

<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="checked='checked' ";
                    }else{
                       $checked = null;
                    }
                }

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

            ?>

        </fieldset>

Any help appreciated. 任何帮助表示赞赏。

After much testing, I figured out the problem why the catID and postID was not inserting ... 经过大量测试,我发现了为什么catID和postID没有插入的问题...

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

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

        //collect form data
        extract($_POST);

I did not include this code in my question, and I found that If I commented out: 我没有在问题中包含此代码,并且发现如果我注释掉了:

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

            //collect form data
            extract($_POST);*/

... the catID and postID was correctly inserting into the DB. ... catID和postID已正确插入数据库中。

It seems that I was sending the value of the $_POST array to the stripslashes function, but it was preventing the data getting inserted into the database. 似乎我正在将$ _POST数组的值发送到stripslashes函数,但是这阻止了将数据插入数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM