false布尔值语句未插入数据库

sc4hvdpw  于 2021-06-24  发布在  Mysql
关注(0)|答案(2)|浏览(413)

我有一个json数组,如下所示:

{
    "operation": "update_original_team_member",
    "teamMember": {
        "teamMemberPresent": false,
        "unique_id": "5a5b7f6b835408.50059003",
        "user_unique_id": "59bea8b7d56a63.33388595"
    }
}

我试着把它插入mysql数据库,但是每次都失败了,但是当我把false改为true时,它就成功了,你知道为什么吗?
插入的表格如下:

CREATE TABLE team_member (
    team_member_id int(11) NOT NULL AUTO_INCREMENT,
    unique_id varchar(23) NOT NULL,
    fullName varchar(50) NOT NULL,
    user_unique_id varchar(23) NOT NULL,
    present boolean NOT NULL,
    date_last_present datetime DEFAULT NULL,
    points int(50) NULL,
    team_id int (11) NOT NULL,
     PRIMARY KEY (team_member_id),
     FOREIGN KEY (`team_id`)
    REFERENCES `ocidb_CB9s149919`.`team` (`team_id`)
    );

json数组是从Reformation(Reformation是android的rest客户端)发送的,接收方式如下:

if ($operation == 'update_original_team_member') {

            if (isset($data -> teamMember) && !empty($data -> teamMember)&& isset($data -> teamMember -> teamMemberPresent)
                && isset($data -> teamMember -> unique_id)&& isset($data -> teamMember -> user_unique_id)){

              $teamMember                 = $data             -> teamMember;
              $teamMemberPresent          = $teamMember       -> teamMemberPresent;
              $unique_id                  = $teamMember       -> unique_id;
              $user_unique_id             = $teamMember       -> user_unique_id;

              echo $fun -> updatePresent($teamMemberPresent,$unique_id, $user_unique_id);

              } else {

              echo $fun -> getMsgInvalidParam();

            }

这将传递给以下函数:

public function updatePresent($teamMemberPresent,$unique_id, $user_unique_id){

     $db = $this -> db;

     if (!empty ($teamMemberPresent)&& !empty ($unique_id)&& !empty ($user_unique_id)){

         $result = $db -> updatePresent($teamMemberPresent,$unique_id, $user_unique_id);

         if ($result) {

            $response["result"] = "success";
            $response["message"] = "Active Students Have Been Recorded!";
            return json_encode($response);

         } else {

            $response["result"] = "failure";
            $response["message"] = "Unable To Update Details, Please Try Again";
            return json_encode($response);

         }
         } else {

      return $this -> getMsgParamNotEmpty();

            }

}

我用来更新的查询是:

public function updatePresent($teamMemberPresent,$unique_id, $user_unique_id){

$sql = "UPDATE team_member SET present = :present WHERE unique_id = :unique_id AND user_unique_id = :user_unique_id";

// Prepare statement
$query = $this ->conn ->prepare($sql);

// execute the query
$query->execute(array(':present' => $teamMemberPresent,':unique_id' => $unique_id, ':user_unique_id' => $user_unique_id));

if ($query) {

return true;        

} else {

return false;

    }
 }

我用 Postman 试图把问题隔离开来,我把范围缩小到 $teamMemberPresent 变量失败,说明它是空的,当它是错的,但我不知道为什么。如前所述,当设置为true时,查询工作正常。
任何指导都将不胜感激。

nafvub8i

nafvub8i1#

将布尔变量转换为int。

$query->execute(array(':present' => (int) $teamMemberPresent,':unique_id' => $unique_id, ':user_unique_id' => $user_unique_id));
// this one -> (int) $teamMemberPresent
bhmjp9jg

bhmjp9jg2#

在你的 updatePresent() 函数时,此代码不会将数据识别为有效数据,并跳过数据库更新:

if (!empty ($teamMemberPresent)&& !empty ($unique_id)&& !empty ($user_unique_id)){
 ...

原因是 empty($teamMemberPresent) 如果其参数具有 false 价值观。
http://php.net/empty 说:
如果变量不存在或其值等于false,则认为该变量为空。
你应该使用 isset($teamMemberPresent) 相反。
下面是一个演示:

if (isset($data -> teamMember)
&& !empty($data -> teamMember)
&& isset($data -> teamMember -> teamMemberPresent)
&& isset($data -> teamMember -> unique_id)
&& isset($data -> teamMember -> user_unique_id))
{       
        echo "Should all be OK\n";  

        $teamMember                 = $data             -> teamMember;
        $teamMemberPresent          = $teamMember       -> teamMemberPresent;

        if (!empty($teamMemberPresent)) {
                echo "Confirmed, not empty\n";
        } else {
                echo "Woops! It's empty\n";
        }
}

输出:

Should all be OK
Woops! It's empty

相关问题