尝试将新记录插入数据库时遇到问题。我认为问题在于比特字段。当我赋值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);
答案 0 :(得分:3)
我假设active
字段属于bit
数据类型。
您不会在为活动字段传递的值周围使用任何引号,就像您对customerid字段所做的那样。
此外,我认为您必须将值 true / false 转换为 1 / 0 。< / p>
修改后的代码:注意到" . $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中针对数据库手动运行它。