PHP没有发布到MYSQL数据库中

时间:2016-02-15 21:22:24

标签: php mysql

您好,我只是一个简单的问题,我试图从复选框中获取一个类别以发布到我的数据库,但它不想玩游戏。我发布了一些内容,我们会看看你们中任何一个可爱的人是否能看到问题。

我的表:

CREATE TABLE `blog_post_cats` (
    `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
    `postID` int(11) DEFAULT NULL,
    `catID` int(11) DEFAULT NULL,
    PRIMARY KEY (`id`)
);

在php中:

//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
        ));
    }
}

复选框:

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

我确实看到了这些类别,但是当我发布它时,请不要将它添加到帖子中,任何帮助都将非常感激。

1 个答案:

答案 0 :(得分:2)

您的问题似乎从if声明开始。

//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
    ));
 }
}

应该是

//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
       ));
     }
}