PHP ~ 列数与第 1 行的值数不匹配
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11989112/
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
PHP ~ Column count doesn't match value count at row 1
提问by Will
I'm creating a registration and I'm using just straight PHP not JavaScript to send my form to the MySQL database, so everything is working fine, no syntax error or anything but I fill out all my information and click 'Register' and it returns a message saying 'Column count doesn't match value count at row 1'.
我正在创建一个注册,我只是直接使用 PHP 而不是 JavaScript 将我的表单发送到 MySQL 数据库,所以一切正常,没有语法错误或任何东西,但我填写了我的所有信息,然后单击“注册”返回一条消息,指出“列计数与第 1 行的值计数不匹配”。
I'm only 14 so this is pretty confusing for me does anyone have a solution?
我只有 14 岁,所以这对我来说很困惑有没有人有解决方案?
This is my INSERT INTO code:
这是我的 INSERT INTO 代码:
$sql = mysql_query("INSERT INTO users(firstname, lastname, email, password, day, month, year, gender)
VALUES('$firstname','$lastname','$email','$db_password','$day','$month','$year')")
or die (mysql_error());
回答by dchacke
You are trying to insert 7 values into 8 columns - you are missing the insertion of gender.
您正在尝试将 7 个值插入 8 列 - 您错过了性别的插入。
The correct code would be:
正确的代码是:
$sql = mysql_query("INSERT INTO users(firstname, lastname, email, password, day, month, year, gender)
VALUES('$firstname','$lastname','$email','$db_password','$day','$month','$year', '$gender')")
or die (mysql_error());
By the way, if you are not already doing it, I highly recommend escaping the strings first, before passing them to the query like so:
顺便说一句,如果您还没有这样做,我强烈建议先转义字符串,然后再将它们传递给查询,如下所示:
$firstname=mysql_real_escape_string($firstname)
You should do this with all variables above. Hereyou can find more about the escape function.
您应该对上述所有变量执行此操作。在这里您可以找到有关转义功能的更多信息。
回答by Bart Friederichs
With your code there, I see you forget to insert $gender.
有了你的代码,我看到你忘记插入 $gender。
When inserting data into a MySQL table, you will have to specify which data goes into which column. You do this by specifying the column names before the VALUES part:
将数据插入 MySQL 表时,您必须指定哪些数据进入哪一列。您可以通过在 VALUES 部分之前指定列名来做到这一点:
INSERT INTO tblA (col1, col2) VALUES ('value1','value2');
If you omit that information, MySQL will expect all columns:
如果省略该信息,MySQL 将期望所有列:
If your table is like this:
如果你的表是这样的:
CREATE TABLE tblA (
col1 INT,
col2 INT
);
You can insert information like this:
您可以插入这样的信息:
INSERT INTO tblA VALUES ('value1', 'value2');
If you omit column names and do no specify values for all columns, MySQL will give the "Column count doesn't match value count at row" error will occur, as MySQL doesn't know what to put in the missing columns. With the table structure as above,
如果省略列名并且没有为所有列指定值,MySQL 将给出“列计数与行的值计数不匹配”错误,因为 MySQL 不知道在丢失的列中放置什么。有了上面的表结构,
INSERT INTO tblA VALUES ('value1');
will result in that error.
将导致该错误。
In non-strict mode, MySQL will insert default values for omitted column names.
在非严格模式下,MySQL 将为省略的列名插入默认值。
回答by Mariusz Sakowski
You have missed a value for gender column (the last one)
您错过了性别列的值(最后一个)
回答by Jocelyn
You are completely missing the gender value:
您完全缺少性别值:
$sql = mysql_query("INSERT INTO users(firstname, lastname, email, password, day, month, year, gender)
VALUES('$firstname','$lastname','$email','$db_password','$day','$month','$year', 'gender goes here')")
or die (mysql_error());

