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

Patricia Arquette
发布: 2024-10-27 10:20:31
原创
167 人浏览过

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中文网其他相关文章!

来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!