简体   繁体   English

当表具有默认列返回时PDO插入列数与第1行的值数不匹配

[英]PDO Inserting when table has default columns returns Column count doesn't match value count at row 1

I've created a database table of four columns, where two of the four are columns to not be updated. 我创建了一个包含四列的数据库表,其中四列中的两列是不更新的列。 For example 例如

Database columns: 数据库列:

  • sessionID = auto increment mysql column sessionID =自动增加mysql列
  • userID = data to be passed in userID =要传递的数据
  • IP = data to be passed in IP =要传递的数据
  • timeSession = default value of CURRENT_TIMESTAMP() in mysql column timeSession = mysql列中CURRENT_TIMESTAMP()默认值

So since two of these columns are to be filled with data, and two are not, I am only inserting the two values needed like so using PDO: 因此,由于其中两列将用数据填充,而两列则不会,因此,我仅使用PDO插入所需的两个值:

$d = [
   'userID' => $userID,
   'IP' => Helper::getIP()
];
$qry = "INSERT INTO {$this->tableName} VALUES (:userID,:IP)";
        $sth = $this->getConnection()->prepare($qry);
        $sth->execute($d);

When running this query, I get the following error: Column count doesn't match value count at row 1 . 运行此查询时,出现以下错误: Column count doesn't match value count at row 1 I have to manually set values for both sessionID and timeSession to make my insert work - which defeats the entire purpose of having an auto incrementing field and a field that auto populates in my database. 我必须手动设置sessionIDtimeSession值才能使插入工作- timeSession了在数据库中具有自动递增字段和自动填充字段的整个目的。

Do I have to modify my PDO logic to account for these two "special" tables? 我是否需要修改我的PDO逻辑以解决这两个“特殊”表? What am I missing here? 我在这里想念什么?

You have four columns, but you specified only 2 in the VALUES section. 您有四列,但是在VALUES部分中仅指定了2列。 You need to list their names after the table name and omit the ones which should be defaulted. 您需要在表名之后列出它们的名称,并省略应默认的名称。

INSERT INTO {$this->tableName} (userID, IP) VALUES (:userID,:IP)

If you want to list the default columns, but let the values be defaulted then you can use the keyword DEFAULT 如果要列出默认列,但让这些值成为默认值,则可以使用关键字DEFAULT

INSERT INTO {$this->tableName} (userID, IP, timeSession ) VALUES (:userID, :IP, DEFAULT)

When inserting a new row, the default value for a column with an expression default can be inserted either by omitting the column name or by specifying the column as DEFAULT (just as for columns with literal defaults) - MySQL Docs 插入新行时,可以通过省略列名或将列指定为DEFAULT来插入具有表达式默认值的列的默认值(就像具有文字默认值的列一样) -MySQL Docs

I like the alternative INSERT syntax that MySQL supports: 我喜欢MySQL支持的替代INSERT语法:

INSERT INTO {$this->tableName} SET userID = :userID, IP = :IP

It's easier to read, matches up the columns with the parameter placeholders, and it also solves the issue you're having when you don't want to specify a value for certain columns. 它更易于阅读,将列与参数占位符匹配,并且还解决了当您不想为某些列指定值时遇到的问题。

The downsides are: 缺点是:

  • It's non-standard syntax, not supported by most other SQL brands. 这是非标准语法,大多数其他SQL品牌均不支持。
  • It doesn't support inserting multiple rows in a single INSERT statement. 它不支持在单个INSERT语句中插入多行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM