2013-05-05 95 views
0

我想创建一个基本的博客,我已经完全按照以前的项目插入到mysql数据库的语法,但是这不会添加。我回应了值,看看它是否正确传递,但是当我检查我的数据库时,什么都没有添加。有没有人可以看到我的代码有问题?无论你是否能够提供帮助,都要事先感谢任何回答问题的人。PDO没有插入数据库,但有正确的值

编辑:我也检查了我的数据库,它工作正常,并且是named,也connect.php适用于我的登录,因为信息是相同的,它应该在这里工作。

EDIT2:数据库表如下

postid int(10) auto_increment 

title text 

author text 

date  date 

content text 

tag1  text 

tag2  text 

<?php 

// Make sure the user is logged in 
session_name('blog'); 
session_start(); 
if (empty($_SESSION['username'])) 
{ 
    header("Location: loginhome.php"); 
    exit; 
} 

// Connect to the database and add a message 
include("connect.php"); 

$one = $_POST['title']; 
$two = $_POST['author']; 
$three = $_POST['content']; 
$four = $_POST['cat1']; 
$five = $_POST['cat2']; 
echo $two; 


$add_message_query = $db->prepare(" 
    INSERT INTO `blogposts` 
     (`title`, `author`, `date`, `content`, `tag1`, `tag2`) 
    VALUES 
     (:title, :author, CURRENT_TIMESTAMP, :content, :cat1, :cat2) 
    "); 

$add_message_query->execute(
    array(
    ':author' => $one, 
    ':title' => $two, 
    ':content' => $three, 
    ':cat1' => $four, 
    ':cat2' => $five 
    ) 
); 
//go to home to show new post 
//header("Location: home.php"); 
?> 
+0

你用'的var_dump($ DB-> ERRORINFO())'和'的var_dump($ add_message_query-> ERRORINFO())'得到什么? – andrewsi 2013-05-05 01:32:21

+1

你确实注意到你将'$ _POST ['title']'作为':author'插入了吗? 此外,我个人更喜欢'NOW()'在'CURRENT_TIMESTAMP'和'varchar'上方的'text'字段中少于500个字符,如标题和作者。 – Thorbear 2013-05-05 01:33:52

+0

asfasfsarray(3){[0] => string(5)“23000”[1] => int(1048)[2] => string(28)“列'tag1'不能为空”当我做var_dump($ add_message_query-> ErrorInfo()),我没有看到我切换非常感谢你 – Chris 2013-05-05 01:39:17

回答