首页  >  文章  >  后端开发  >  为什么我在 MySQL 查询中收到“第 1 行列计数不匹配”错误?

为什么我在 MySQL 查询中收到“第 1 行列计数不匹配”错误?

Patricia Arquette
Patricia Arquette原创
2024-10-27 10:20:31168浏览

Why am I getting the

PHP、MySQL 错误:第 1 行的列计数不匹配

此错误“列计数与第 1 行的值计数不匹配” ",表示您尝试插入数据的列数与提供的实际值数之间存在差异。

在提供的代码片段中,您尝试使用以下命令将数据插入到表中9 列(id、名称、描述、shortDescription、成分、方法、长度、dateAdded、用户名),使用 8 个值($name、$description、$shortDescription、$ingredients、$method、$username、$length、$dateAdded)。

错误原因:

发生错误的原因是 INSERT 查询中定义的列数与传递给它的值的数量不匹配。在本例中,查询需要 9 个值,但只提供了 8 个值,不包括“image”列。

解决方案:

要解决该错误,请确保值的数量与 INSERT 查询中的列数相匹配。您可以添加“图像”列的缺失值,或从要插入的列列表中将其删除。

代码修改:

选项1:添加缺失值

<code class="php">$query = sprintf("INSERT INTO dbname (id, Name, Description, shortDescription, Ingredients, Method, Image, Length, dateAdded, Username) VALUES ('', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')",
    mysql_real_escape_string($name),
    mysql_real_escape_string($description),
    mysql_real_escape_string($shortDescription),
    mysql_real_escape_string($ingredients),
    mysql_real_escape_string($method),
    '', // Placeholder for empty 'image' value
    mysql_real_escape_string($length),
    mysql_real_escape_string($dateAdded),
    mysql_real_escape_string($username));</code>

选项2:删除“图像”列

<code class="php">$query = sprintf("INSERT INTO dbname (id, Name, Description, shortDescription, Ingredients, Method, Length, dateAdded, Username) VALUES ('', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')",
    mysql_real_escape_string($name),
    mysql_real_escape_string($description),
    mysql_real_escape_string($shortDescription),
    mysql_real_escape_string($ingredients),
    mysql_real_escape_string($method),
    mysql_real_escape_string($length),
    mysql_real_escape_string($dateAdded),
    mysql_real_escape_string($username));</code>

以上是为什么我在 MySQL 查询中收到“第 1 行列计数不匹配”错误?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn