2017-02-22 112 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