2017-08-08 242 views
-1

我正在尝试后我定制的社交网络时,这个错误...:致命错误:未捕获PDOException:SQLSTATE [21S01]

PHP Fatal error: Uncaught PDOException: SQLSTATE[21S01]: Insert value list does not match column list: 1136 Column count doesn't match value count at row 1 in /home//classes/DB.php:10

Stack trace:
0 /home//classes/DB.php(10): PDOStatement->execute(Array)
1 /home//classes/Post.php(13): DB::query('INSERT INTO pos...', Array)
2 /home//profile.php(56): Post::createPost('test post again', '1', '1')
3 {main}
thrown in /home/progreen/thebirding.space/classes/DB.php on line 10

我的表结构是这样的:

CREATE TABLE posts (
id int(11) unsigned NOT NULL AUTO_INCREMENT,
body varchar(160) NOT NULL DEFAULT '',

posted_at datetime NOT NULL,
user_id int(11) unsigned NOT NULL,
likes int(11) unsigned NOT NULL,
postimg varchar(255) DEFAULT NULL,
topics varchar(400) DEFAULT NULL,
PRIMARY KEY (id),
KEY user_id (user_id),

CONSTRAINT posts_ibfk_1 FOREIGN KEY (user_id) REFERENCES users (id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

而且从post.php中的功能看起来像这样...:

public static function createPost($postbody, $loggedInUserId, $profileUserId) { 
    if (strlen($postbody) > 220 || strlen($postbody) < 1) { 
     die('Incorrect length!'); 
    } 
    if ($loggedInUserId == $profileUserId) { 
     DB::query('INSERT INTO posts VALUES (\'\', :postbody, NOW(), :userid, 0, \'\')', array(':postbody'=>$postbody, ':userid'=>$profileUserId)); 
    } else { 
     die('Incorrect user!'); 
    } 
    } 

这是工作完全正常和帖子当时正在进入编入数据库的罚款,直到我加入了createImgPost功能,这是... ...

public static function createImgPost($postbody, $loggedInUserId, $profileUserId) { 
    if (strlen($postbody) > 220) { 
     die('Incorrect length!'); 
    } 
    if ($loggedInUserId == $profileUserId) { 
     DB::query('INSERT INTO posts VALUES (\'\', :postbody, NOW(), :userid, 0, \'\')', array(':postbody'=>$postbody, ':userid'=>$profileUserId)); 
     $postid = DB::query('SELECT id FROM posts WHERE user_id=:userid ORDER BY ID DESC LIMIT 1;', array(':userid'=>$loggedInUserId))[0]['id']; 
     return $postid; 
    } else { 
     die('Incorrect user!'); 
    } 
    } 

我失去了一些东西明显这里的家伙?如果需要,我很高兴提供更多信息和代码示例!非常感谢!

+3

提供的值数与表中的列数不匹配。你应该明确地告诉SQL哪些列被插入的原因之一是例如'INSERT INTO \'table \'(\'column1 \',\'column2 \'...)VALUES(...)'。 –

+1

通过使用双引号和单引号而不是像这样'DB :: query(“INSERT INTO posts VALUES('',:postbody,NOW(),:userid,0')来简化查询字符串文字“,array(':postbody'=> $ postbody,':userid'=> $ profileUserId));' – RiggsFolly

+0

已经尝试过@RiggsFolly和stillg等同样的结果,我不敢说! –

回答

1

您需要在插入查询中传递postimg值以匹配列。

相关问题