2014-10-29 58 views
0

我已經加入另一位開發人員使用Zend Framework處理項目,並且我無法獲得任何數據庫插入語句在本地機器上工作。出於測試目的,我使用root用戶,我可以讓所有其他語句工作。Zend Framework - 由於空的主鍵導致PDO插入不工作

我的配置是nginx 1.6.2,PHP版本5.5.18和PDO Mysql API版本5.6.21。

服務器配置的Apache 2.0,PHP版本5.4.22和PDO Mysql的API版本34年5月5日

的問題是每一次我嘗試插入一行到數據庫中,我得到一次這樣

錯誤
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 1366 Incorrect integer value: '' for column 'id_client' at row 1' 

在這種情況下,我嘗試使用下面的代碼,這是Zend的非常標準的中插入一行到$tableclient

$this->_db->insert ($table, $values); 
$id = $this->_db->lastInsertId(); 

這是數組$values我試圖插入

array (size=13) 
    'id_client' => string '' (length=0) 
    'id_profile' => string '2' (length=1) 
    'status' => string '1' (length=1) 
    'client' => string 'ABC' (length=3) 
    'contact' => string 'Fred' (length=4) 
    'phone' => string '0412345678' (length=10) 
    'address' => string 'Sydney Rd' (length=6) 
    'suburb' => string 'Sydney' (length=6) 
    'state' => string 'New South Wales' (length=15) 
    'post_code' => string '2000' (length=4) 
    'notas' => string '' (length=0) 
    'bg' => string '#FEAF1D' (length=7) 
    'logo' => string '' (length=0) 

這是表client數據庫結構,顯示了在年底

CREATE TABLE IF NOT EXISTS `client` (
    `id_client` int(20) NOT NULL, 
    `client` char(100) COLLATE utf8_spanish_ci DEFAULT NULL, 
    `contact` char(20) COLLATE utf8_spanish_ci DEFAULT NULL, 
    `phone` char(20) COLLATE utf8_spanish_ci DEFAULT NULL, 
    `address` char(50) COLLATE utf8_spanish_ci DEFAULT NULL, 
    `suburb` char(30) COLLATE utf8_spanish_ci DEFAULT NULL, 
    `state` char(20) COLLATE utf8_spanish_ci DEFAULT NULL, 
    `post_code` char(6) COLLATE utf8_spanish_ci DEFAULT NULL, 
    `county` char(20) COLLATE utf8_spanish_ci DEFAULT NULL, 
    `notas` text COLLATE utf8_spanish_ci, 
    `logo` varchar(200) COLLATE utf8_spanish_ci DEFAULT NULL, 
    `bg` char(10) COLLATE utf8_spanish_ci DEFAULT NULL, 
    `id_profile` int(2) DEFAULT NULL 
) ENGINE=MyISAM AUTO_INCREMENT=10 DEFAULT CHARSET=utf8 COLLATE=utf8_spanish_ci; 

ALTER TABLE `client` 
ADD PRIMARY KEY (`id_client`), ADD KEY `id_profile` (`id_profile`); 

正如你可以在看到主鍵id_client$values數組沒有值id_client這就是爲什麼我得到錯誤,但在zend框架(和其他人)插入語句應仍然工作,因爲數據庫將自動分配一個基於AUTO_INCREMENT的值。這就是它在服務器上的工作方式,但是我的本地主機上沒有這樣做。

任何幫助,將不勝感激。

+1

從陣列中刪除id_client – dixromos98 2014-10-29 07:19:44

回答

1

從您試圖插入的值列表中刪除client_id列,因爲包括它會使數據庫嘗試插入您提供的值(並且空白值是一個值)。

其次,您需要更改id_client` int(20) NOT NULL,` to id_client int(20) NOT NULL AUTO_INCREMENT,的列定義,以便數據庫知道如何確定插入時該列的正確值。

2

確保您的id_client爲AUTO_INCREMENT,您的表定義似乎沒有定義自動增量字段。

其次,@ dixromos98提到從數組中刪除id_client。