php – 如何将数据插入位数据类型的字段?

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 如何将数据插入位数据类型的字段?脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
尝试将新记录插入数据库时​​遇到问题.我认为问题在于比特字段.当我赋值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. ) )

但如果我把它改成假就行了.我将展示一些我的代码.由于我已将其缩小到此变量,因此我已将其中的大部分缩减:

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

解决方法

我假设活动字段是位数据类型.

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

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

修改后的代码:注意到“.$active.”周围的单引号已被删除.

$sqlInsert = "INSERT INTO customers(customerID,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中针对数据库手动运行它.

脚本宝典总结

以上是脚本宝典为你收集整理的php – 如何将数据插入位数据类型的字段?全部内容,希望文章能够帮你解决php – 如何将数据插入位数据类型的字段?所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。