【问题标题】:PHP Record Not Inserting into MySql Blog Categories TablePHP 记录未插入 MySql 博客类别表
【发布时间】:2016-08-19 01:43:06
【问题描述】:

我有一个 addpost.php,它有一个将博客文章提交到 mysql 数据库的表单。这些表格是:

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

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

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

当用户提交表单时,帖子标题、内容、描述会发布到 blog_posts_seo 表中,而且所选类别也会将 catID 和 postID 传递到 blog_post_cats 表中。下面是表单域的示例。还有如何通过查询 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>

所以如果提交按钮被按下,插入语句就会被执行:

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;

实际发生的事情是没有任何东西被传递到 blog_post_cats 表!所以我不确定出了什么问题?

我认为插入语句对于将 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>

任何帮助表示赞赏。

【问题讨论】:

    标签: php html mysql forms


    【解决方案1】:

    经过多次测试,我找到了为什么 catID 和 postID 没有插入的问题......

    if(isset($_POST['submit'])){
    
            $_POST = array_map( 'stripslashes', $_POST );
    
            //collect form data
            extract($_POST);
    

    我的问题中没有包含这段代码,我发现如果我注释掉了:

    /*$_POST = array_map( 'stripslashes', $_POST );
    
                //collect form data
                extract($_POST);*/
    

    ... catID 和 postID 已正确插入数据库。

    似乎我将 $_POST 数组的值发送到了 stripslashes 函数,但它阻止了数据插入到数据库中。

    【讨论】:

      猜你喜欢
      • 2013-12-31
      • 2020-02-25
      • 2011-08-21
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      • 1970-01-01
      • 2017-11-16
      • 2012-07-12
      相关资源
      最近更新 更多