2017-08-08 244 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值以匹配列。

相關問題