4

尝试将新记录插入数据库时​​遇到问题。我认为问题在于位域。当我分配值 True 我得到这个错误:

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);
4

1 回答 1

3

我假设该active字段是bit数据类型。

您不要在为活动字段传递的值周围使用任何引号,就像您为 customerid 字段所做的那样。

另外,我认为您必须将值true / false转换为1 / 0

修改代码:注意到周围的单引号" . $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);

我不确定为什么它与False值一起使用。我建议您在设置所有值后找出INSERT语句的评估方式。不要执行语句,而是将 INSERT 语句打印到屏幕/页面并在 SQL Server Management Studio 中针对数据库手动运行它。

于 2012-04-29T12:29:48.930 回答