加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 16:45:03 所属栏目:PHP教程 来源:网络整理
导读:尝试将新记录插入数据库时??遇到问题.我认为问题在于比特字段.当我赋值True时,我得到这个错误: Failed: Array ( [0] = Array ( [0] = 22018 [SQLSTATE] = 22018 [1] = 245 [code] = 245 [2] = [Microsoft][SQL Server Native Client 10.0][SQL Server]Conve
尝试将新记录插入数据库时??遇到问题.我认为问题在于比特字段.当我赋值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中针对数据库手动运行它.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读