PDO未捕获的异常-插入值列表与列列表不匹配
问题描述:
我收到此异常
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
使用以下代码:
$stmt = $conn->prepare('INSERT INTO project VALUES(:category, :title, :name)');
$stmt->execute(array(
':category' => $_POST['category'],
':title' => $_POST['title'],
':name' => $_POST['name']
));
错误消息是什么意思?
答
在查询中,指定要填充的列,例如:
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.