2017-02-22 117 views
0

我的问题是如此奇怪。 我想在mysql表中插入一行(使用InnoDB)。没有错误。一切都很好。但该行未添加到表中。Codeigniter 3插入查询失败

UPDATE:

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

OR

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

我试图删除停车表并重新创建它,但没有成功。 我也尝试创建另一张具有相同结构和不同名称的表(例如parkingsss),但是没有成功。

+0

你已经在你的'配置/数据库启用错误报告.php“ – RiggsFolly

+0

https://codeigniter.com/userguide3/database/transactions.html – RiggsFolly

+0

是的。没有错误。我试过使用: $ this-> db-> trans_begin(); &$ this-> db-> trans_commit(); ,但没有成功 –

回答

-1

这似乎是因为你在id,userId和listId中使用String值。尝试将它变成

$parkings = array (
    'userId' => 27193, 
    'listId' => 14, 
    'createDate' => '2017-02-23', 
    'alertReq' => 1 
); 
+0

没有成功。使用字符串或整数我会得到相同的结果:( –

0

尝试跨像https://codeigniter.com/userguide3/database/transactions.html#running-transactions-manually

使用一个dB设置()之类的代码如下少行,然后

$this->db->trans_begin(); 

$data = array(
    'userId' => '27193', 
    'listId' => '14', 
    'createDate' => '2017-02-23', 
    'alertReq' => '1' 
); 

$this->db->set($data); 
$query = $this->db->insert('parking'); 

if ($this->db->trans_status() === FALSE) 
{ 
    $this->db->trans_rollback(); 

} else { 

    $this->db->trans_commit(); 
} 

return $query; // Returns only true or false; 
+0

谢谢你。我现在尝试你的代码。但它没有工作。但ID自动增量加1,但行不添加! –

+0

add return false回滚后,你会看到查询失败 – RaymondM