PHP,错误 1136:列数与第 1 行的值数不匹配

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5616871/
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 22:00:39  来源:igfitidea点击:

PHP, Error 1136 : Column count doesn't match value count at row 1

phpmysql

提问by user701565

I get this Exception:

我得到这个异常:

Error 1136 : Column count doesn't match value count at row 1

Structure of the table :

表的结构:

create table gb_entries (
    id int(4) not null auto_increment,
    username varchar(40) not null,
    name varchar(40),
    gender varchar(40),
    dob int(40),
    email varchar(40),
    primary key (id) 
);

With this PHP code:

使用此 PHP 代码:

// Add a new entry to the database
function addEntry($username, $name, $gender, $dob, $email) {
  $connection = mysql_open();
  $insert = "insert into gb_entries " . 
      "values ('$username', '$name', '$gender', '$dob', '$email')";
  $result = @ mysql_query ($insert, $connection)
      or showerror();
  mysql_close($connection)
      or showerror();
}

// Return an array of database entries that contain $name anad $email
function getEntries($username,$name,$gender,$dob,$email) {
  // Sanitise user input to prevent SQL injection attacks
  $username = mysql_escape_string($username);
  $name = mysql_escape_string($name);
  $gender = mysql_escape_string($gender);
  $dob = mysql_escape_string($dob);
  $email = mysql_escape_string($email);


  // Open connection and select database
  $connection = mysql_open();

  // Construct query
  $query = 
    "select username, name, gender, dob, email from gb_entries where 0=0 ";
  if (! empty($username)) {
      $query .= "AND username LIKE '%$username%' ";
  }
   if (! empty($name)) {
      $query .= "AND name LIKE '%$name%' ";
  }
  if (! empty($gender)) {
      $query .= "AND gender LIKE '%$gender%' ";
  }
  if (! empty($dob)) {
      $query .= "AND dob LIKE '%$dob%' ";
  }
  if (! empty($email)) {
      $query .= "AND email LIKE '%$email%' ";
  }


  $query .= "ORDER BY id";
  // echo $query;

  // Execute query
  $result = @ mysql_query($query, $connection)
      or showerror();

  // Transform the result set to an array (for Smarty)
  $entries = array();
  while ($row = mysql_fetch_array($result)) {
      $entries[] = $row;
  }

  mysql_close($connection)
      or showerror();
  return $entries;
}

What does the Exception mean?

异常是什么意思?

回答by AgentConundrum

As it says, the column count doesn't match the value count. You're providing five values on a six column table. Since you're not providing a value for id, as it's auto increment, it errors out - you need to specify the specific columns you're inserting into:

正如它所说,列数与值数不匹配。您在六列表上提供五个值。由于您没有为 提供值id,因为它是自动递增的,因此会出错 - 您需要指定要插入的特定列:

$insert = "insert into gb_entries (username, name, gender, dob, email) " . 
      "values ('$username', '$name', '$gender', '$dob', '$email')"

Also, I really hate that WHERE 0=0line. I know whyyou're doing it that way, but I personally find it cleaner to do something like this (warning: air code!):

另外,我真的很讨厌那条WHERE 0=0线。我知道为什么你做这样的说法,但我个人觉得清洁剂做这样的事情(警告:空气代码):

$query = "select username, name, gender, dob, email from gb_entries ";

$where = array();
if (! empty($username)) {
      $where[] = "username LIKE '%$username%'"; // add each condition to an array
// repeat for other conditions

// create WHERE clause by combining where clauses, 
// adding ' AND ' between conditions, 
// and append this to the query if there are any conditions 
if (count($where) > 0) {
  $query .= "WHERE " . implode($where, " AND "); 
}

This is personal preference, as the query optimizer would surely strip out the 0=0on it's own and so it wouldn't have a performance impact, but I just like my SQL to have as few hacks as possible.

这是个人偏好,因为查询优化器肯定会0=0自行剥离它,因此不会对性能产生影响,但我只是希望我的 SQL 尽可能少地进行黑客攻击。

回答by Pascal MARTIN

If the error is occurring when trying to insert a row to your table, try specifying the list of fields, in the insert query -- this way, the number of data in the valuesclause will match the number of expected columns.

如果在尝试向表中插入行时发生错误,请尝试在插入查询中指定字段列表——这样,values子句中的数据数将与预期的列数相匹配。

Else, MySQL expects six columns : it expects the idcolumn -- for which you didn't specify a value.

否则,MySQL 需要 6 列:它需要id您未指定值的列。


Basically, instead of this :


基本上,而不是这个:

$insert = "insert into gb_entries " . 
  "values ('$username', '$name', '$gender', '$dob', '$email')";

Use something like that :

使用类似的东西:

$insert = "insert into gb_entries (username, name, gender, dob, email) " . 
  "values ('$username', '$name', '$gender', '$dob', '$email')";

回答by pkanane

I had a similar problem. The column count was correct. the problem was that i was trying to save a String (the value had quotes around it) in an INT field. So your problem is probably coming from the single quotes you have around the '$dob'. I know, the mysql error generated doesn't make sense..

我有一个类似的问题。列数是正确的。问题是我试图在 INT 字段中保存一个字符串(该值周围有引号)。所以你的问题可能来自你在'$dob'周围的单引号。我知道,生成的 mysql 错误没有意义..

funny thing, I had the same problem again.. and found my own answer here (quite embarrassingly) It's an UNEXPECTED Data problem (sounds like better error msg to me). I really think, that error message should be looked at again

有趣的是,我又遇到了同样的问题......并在这里找到了我自己的答案(非常尴尬)这是一个意外的数据问题(对我来说听起来像是更好的错误信息)。我真的认为,应该再次查看该错误消息

回答by Chris

Does modifying this line help?

修改这条线有帮助吗?

$insert = "insert into gb_entries (username, name, gender, dob, email) " . 
      "values ('$username', '$name', '$gender', '$dob', '$email')";