Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
996 views
in Technique[技术] by (71.8m points)

php - PDO Uncaught exception - Insert value list does not match column list

I get this Exception

Uncaught exception 'PDOException' with message 'SQLSTATE[21S01]: 
Insert value list does not match column list: 1136 Column count doesn't match 
value count at row 1

With this code:

$stmt = $conn->prepare('INSERT INTO project VALUES(:category, :title, :name)');
      $stmt->execute(array(
          ':category' => $_POST['category'],
          ':title' => $_POST['title'],
          ':name' => $_POST['name']
));

What does the error message mean?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In your query, specify the columns that you want to populate, for example:

$stmt = $conn->prepare('INSERT INTO project (category, title, name) VALUES(:category, :title, :name)');

If you don't specify the columns in that way, you have to include a value for all columns in the table, that's why you're getting the error - because there are other columns in the table and you haven't explicitly specified a value for them all.

It is better to specify the columns because if any columns are added or the order is changed in future, your query will break unless the column list is specified.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...