I am getting this error when trying to post on my custom built social network...:
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
My table structure looks like this:
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
),
KEYuser_id
(user_id
),
CONSTRAINTposts_ibfk_1
FOREIGN KEY (user_id
) REFERENCESusers
(id
)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
And the function from Post.php looks like this...:
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!');
}
}
This was working perfectly fine and posts was being entered into the database fine until I added the createImgPost function which is this...
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!');
}
}
Am I missing something obvious here guys? I'm happy to provide more info and code examples if needed! Many thanks!
You need to pass postimg value in insert query to match the columns.