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

时间:2011-04-11 04:51:18

标签: php mysql

我得到了这个例外:

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

表的结构:

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) 
);

使用此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;
}

异常是什么意思?

4 个答案:

答案 0 :(得分:7)

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

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

另外,我真的很讨厌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 "); 
}

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

答案 1 :(得分:2)

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

否则,MySQL需要六列:它需要id列 - 您没有为其指定值。


基本上,而不是:

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

使用类似的东西:

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

答案 2 :(得分:2)

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

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

答案 3 :(得分:0)

修改此行有帮助吗?

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