2012-04-29 94 views
4

我試圖插入新記錄插入數據庫時​​有問題。我認爲問題在於位域。當我給你的價值真的,我得到這個錯誤:如何將數據插入位數據類型的字段?

Failed: Array ([0] => Array ([0] => 22018 [SQLSTATE] => 22018 [1] 
=> 245 [code] => 245 [2] => [Microsoft][SQL Server Native Client 10.0] 
[SQL Server]Conversion failed when converting the varchar value ' 1 ' 
to data type bit. [message] => [Microsoft][SQL Server Native Client 10.0] 
[SQL Server]Conversion failed when converting the varchar value ' 1 ' to 
data type bit.)) 

,但如果我將其更改爲false它的工作原理。我會顯示一些我的代碼。我把它的大部分縮小了,因爲我已經縮小到這個變量了:

$active = True; 

這裏是我的插入查詢。

$sqlInsert = "INSERT INTO customers(
       customerID, 
       registeredDate, 
       givenName, 
       familyName, 
       email, 
       password, 
       phone, 
       mobile, 
       property, 
       street, 
       locality, 
       town, 
       area, 
       postalCode, 
       active 
      ) 
      VALUES(" . 
       $newUser . "," . 
       $date . ", ' " . 
       $given . " ', ' " . 
       $family . " ', ' " . 
       $email . " ', ' " . 
       $pwd . " ', ' " . 
       $phone . " ', ' " . 
       $mob . " ', ' " . 
       $property . " ', ' " . 
       $street . " ', ' " . 
       $locality . " ' , ' " . 
       $town . " ', ' " . 
       $area . " ', ' " . 
       $postalcode . " ', ' " . 
       $active . " ')"; 

$stmtInsert = sqlsrv_query($conn, $sqlInsert); 

回答

3

我假定active字段是bit數據類型。

不要使用圍繞價值的任何引號傳遞一個像你這樣的客戶ID字段做什麼活動現場。

另外,我覺得你的價值/轉換爲/。

修改後的代碼:注意到圍繞" . $active . "的單引號已被刪除。

$sqlInsert = "INSERT INTO customers(customerID, registeredDate, givenName, 
       familyName, email, password, 
       phone, mobile, property, 
       street, locality, town, 
       area, postalCode, active) 
       VALUES(" . $newUser . "," . $date . ", ' " . $given . " ', 
       ' " . $family . " ', ' " . $email . " ', ' " . $pwd . " ', 
       ' " . $phone . " ', ' " . $mob . " ', ' " . $property . " ', 
       ' " . $street . " ', ' " . $locality . " ' , ' " . $town . " ', 
       ' " . $area . " ', ' " . $postalcode . " ', " . $active . ")"; 
$stmtInsert = sqlsrv_query($conn, $sqlInsert); 

我不知道爲什麼它與價值的工作。我建議您在設置完所有值後,瞭解INSERT語句的計算結果。不是執行語句,而是將INSERT語句打印到屏幕/頁面,並在SQL Server Management Studio中針對數據庫手動運行它。

+0

爲什麼是假的,雖然工作? – 2012-04-29 13:10:15

相關問題