php MySQL 错误:“列数与第 1 行的值数不匹配” - 初学者帮助

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5304613/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 20:55:46  来源:igfitidea点击:

MySQL error: "Column count doesn't match value count at row 1" - beginner help

phpmysql

提问by buymypies

basically, php & MySQL being used. I am a beginner.

基本上,正在使用 php 和 MySQL。我是初学者。

What I am trying to do is registering a user to my database, so storing the form input to my users_tb.

我想要做的是将用户注册到我的数据库,因此将表单输入存储到我的 users_tb。

get this error when I try inserting the values into the form:

当我尝试将值插入表单时出现此错误:

"Column count doesn't match value count at row 1"

“列数与第 1 行的值数不匹配”

I thought it was because I wasn't inserting the user_id value (which is auto increment), so I tried inserting '' in my query for the user_id, but still no luck.

我认为这是因为我没有插入 user_id 值(这是自动增量),所以我尝试在我的 user_id 查询中插入 '',但仍然没有运气。

here is the query:

这是查询:

$query = "INSERT INTO users_tb (user_id, user_status, user_gender, user_firstname, user_surname, student_number,
    user_email, user_dob, user_name, user_pass) 
    VALUES('','$status','$gender','$firstname','$surname','$hnumber','$dob','$username','$password')";
    mysql_query($query) or die(mysql_error());
    mysql_close();

whether that helps. If you need any other code just say.

是否有帮助。如果您需要任何其他代码,请说。

just to make sure though, the inserts don't have to be in same order the fields are in the table do they?

只是为了确保,插入不必与表中字段的顺序相同,对吗?

many thanks,

非常感谢,

回答by Czechnology

You're missing one value.

你缺少一个值。

For queries this long with so many columns (and if you're inserting just one row), I'd suggest using the following INSERT syntax, which is much easier to read and less likely to cause problems.

对于具有如此多列的如此长的查询(如果您只插入一行),我建议使用以下 INSERT 语法,它更易于阅读并且不太可能导致问题。

$query = "INSERT INTO users_tb SET
          user_status    = '". mysql_real_escape_string($status) ."',
          user_gender    = '". mysql_real_escape_string($gender) ."',
          user_firstname = '". mysql_real_escape_string($firstname) ."',
          user_surname   = '". mysql_real_escape_string($surname) ."',
          student_number = '". mysql_real_escape_string($hnumber) ."',
          user_email     = '". mysql_real_escape_string($email) ."',
          user_dob       = '". mysql_real_escape_string($dob) ."',
          user_name      = '". mysql_real_escape_string($username) ."',
          user_pass      = '". mysql_real_escape_string($password) ."'";
mysql_query($query) or die(mysql_error());
mysql_close();

回答by Mark Byers

You are missing a value for user_email.

您缺少 的值user_email

$query = "INSERT INTO users_tb
          (
              user_status,
              user_gender,
              user_firstname,
              user_surname,
              student_number,
              user_email,
              user_dob,
              user_name,
              user_pass
          ) 
          VALUES
          (
              '$status', 
              '$gender',
              '$firstname',
              '$surname',
              '$hnumber',
              '$email',    -- <--- you forgot this!
              '$dob',
              '$username',
              '$password'
          )";

And just a reminder: you should escape the values using mysql_real_escape_stringif you are not already doing so.

只是提醒一下:mysql_real_escape_string如果您还没有这样做,则应该使用 using 来转义这些值。

回答by Ken White

Mark Byers answered the problem part, but didn't address this question:

Mark Byers 回答了问题部分,但没有解决这个问题:

just to make sure though, the inserts don't have to be in same order the fields are in the table do they?

只是为了确保,插入不必与表中字段的顺序相同,对吗?

No, they don't have to be in the same order as the columns in the table, but your columns list and your values list have to match both in count and in data type. The problem you have is the one Mark spotted; you're missing a value for user_email, which means the dobvalue is trying to go in that column instead. MySQL is seeing that there aren't enough values for the columns you listed, and reporting the error back to you.

不,它们不必与表中的列顺序相同,但您的列列表和值列表必须在计数和数据类型上匹配。您遇到的问题是 Mark 发现的问题;您缺少 的值user_email,这意味着该dob值正试图进入该列。MySQL 发现您列出的列没有足够的值,并向您报告错误。

Column count doesn't match value countis actually a pretty clear message, which is unusual for database engines. :)

Column count doesn't match value count实际上是一个非常明确的消息,这对于数据库引擎来说是不寻常的。:)

回答by Your Common Sense

You can use this simple function to create a query out of $_POST array and list of allowed fields:

您可以使用这个简单的函数从 $_POST 数组和允许的字段列表中创建查询:

function dbSet($fields) {
  $set='';
  foreach ($fields as $field) {
    if (isset($_POST[$field])) {
      $set.="`$field`='".mysql_real_escape_string($_POST[$field])."', ";
    }
  }
  return substr($set, 0, -2); 
}

used like this

像这样使用

$table  = "users_tb";
$fields = explode(" ","user_status user_gender user_firstname user_surname student_number user_email user_dob user_name user_pass");
$query  = "INSERT INTO $table SET ".dbSet($fields);
mysql_query($query) or trigger_error(mysql_error()." in ".$query);

of course it's assuming that HTML form field names match SQL table field names, which is very handy consideration

当然是假设 HTML 表单字段名称匹配 SQL 表字段名称,这是非常方便的考虑

回答by konsolenfreddy

you've got 10 fields you want to insert, but you're providing only 9 values

您有 10 个要插入的字段,但您只提供了 9 个值

回答by Joe Stefanelli

You list 10 columns but you only have 9 values.

您列出了 10 列,但只有 9 个值。