为什么MySql没有用php存储布尔值

时间:2018-04-15 12:01:47

标签: php mysql pdo

我有一个数据库表,其中有两列已设置为boolean。每当我在表中存储一些东西时,一切都正常,除了布尔列总是假的。专栏发布并发布。

我在xampp本地安装上使用php 7.1.1和MariaDB 10.1.21。

show table的输出:

CREATE TABLE `posts` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `title` varchar(255) NOT NULL,
    `content` text NOT NULL,
    `seo_description` varchar(255) NOT NULL,
    `featured_post` tinyint(1) NOT NULL DEFAULT '0',
    `published` tinyint(1) NOT NULL DEFAULT '0',
    `seo_title` varchar(255) NOT NULL,
    `post_type` enum('blog','product') NOT NULL,
    `featured_image_id` int(11) NOT NULL,
    `category_id` int(11) NOT NULL,
     PRIMARY KEY (`id`),
     UNIQUE KEY `title` (`title`),
     UNIQUE KEY `seo_title` (`seo_title`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=latin1 ROW_FORMAT=COMPACT

定义变量onr的Php代码,另一个完全相同:

if(isset($_POST["published"])){
    $published = sanitizeInput($_POST["published"]);

    if($published){  //test to see if correct values where being sent and they were
        echo json_encode("boolean true"); 
    } else {
        echo json_encode("boolean false");
    }
} else {
    $published = false;
}


$query = "insert into posts "
        . "(title, content, seo_description, featured_post, published, seo_title, post_type, category_id) "
        . "values "
        . "(:title, :content, :seo_description, :featured_post, :published, :seo_title, :post_type, :category_id)";

$stmt = $pdo->prepare($query);

$stmt->bindValue("featured_post",   $featuredPost,  PDO::PARAM_BOOL);
$stmt->bindValue("published",       $published,     PDO::PARAM_BOOL);
$stmt->bindValue("title",           $title);
$stmt->bindValue("content",         $content);
$stmt->bindValue("seo_description", $seoDescription);
$stmt->bindValue("seo_title",       $seoTitle);
$stmt->bindValue("post_type",       $postType);
$stmt->bindValue("category_id",     $categoryId);

$stmt->execute();   

非常感谢任何帮助。

提前致谢

0 个答案:

没有答案