【发布时间】:2025-12-23 05:05:12
【问题描述】:
我的问题很奇怪。 我正在尝试在 mysql 表中插入一行(使用 InnoDB)。没有错误。一切看起来都很好。但该行未添加到表中。
更新:
将表引擎从 InnoDB 更改为 MyISAM 将解决问题,但为什么 InnoDB 不起作用?
这是我的代码,它总是返回 true:
$this->db->trans_start();
$this->db->set('userId', '27193');
$this->db->set('listId', '14');
$this->db->set('createDate', '2017-02-23');
$this->db->set('alertReq', '1');
$this->db->insert('parking');
if ($this->db->affected_rows() == '1') {
$this->db->trans_complete();
return true;
} else {
$this->db->trans_complete();
return false;
}
我还尝试了不同方式的插入查询:
$parkings = array (
'userId' => '27193',
'listId' => '14',
'createDate' => '2017-02-23',
'alertReq' => '1'
);
$this->db->trans_start();
$this->db->insert('parking', $parkings);
if ($this->db->affected_rows() == '1') {
$this->db->trans_complete();
return true;
} else {
$this->db->trans_complete();
return false;
}
或
$sql = "INSERT INTO `parking` (`userId`, `listId`, `createDate`, `alertReq`) VALUES ('27193', '14', '2017-02-23', '1')";
$query = $this->db->query($sql);
使用时
$this->output->enable_profiler(TRUE);
在我的控制器中,上述所有查询都会生成并显示:
INSERT INTO `parking` (`userId`, `listId`, `createDate`, `alertReq`) VALUES ('27193', '14', '2017-02-23', '1')
在分析器中,甚至在停车表中,id 列也会自动递增 1。 但是表中没有添加新行!!!
当我使用 phpmyadmin 或 adminer 插入该行时,它们按预期工作,我可以看到添加了新行。但是使用 CI,我没有成功!
这是我的表结构:
CREATE TABLE `parking` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`userId` int(11) NOT NULL,
`listId` int(11) NOT NULL,
`createDate` date NOT NULL,
`alertReq` tinyint(1) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
我试图删除 parking 表并重新创建它,但没有成功。 我还尝试创建另一个具有相同结构和不同名称的表(例如 parkingsss),但同样没有成功。
【问题讨论】:
-
您的
config/database.php中是否启用了错误报告 -
是的。没有错误。我尝试使用: $this->db->trans_begin(); & $this->db->trans_commit(); ,但没有成功
-
您有什么证据表明“没有添加新行”?
-
@RickJames phpmyadmin 中没有新行,并且选择查询也没有显示任何内容。
标签: php mysql codeigniter mariadb codeigniter-3