douqudi5282 2017-08-08 08:49
浏览 138

致命错误:未捕获的PDOException:SQLSTATE [21S01]

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),
KEY user_id (user_id),

CONSTRAINT posts_ibfk_1 FOREIGN KEY (user_id) REFERENCES users (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!

  • 写回答

1条回答 默认 最新

  • douaipi3965 2017-08-08 08:53
    关注

    You need to pass postimg value in insert query to match the columns.

    评论

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示