不插入数据库的错误布尔值语句

时间:2018-01-14 19:41:32

标签: php mysql prepared-statement

我有一个看起来像这样的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阵列是从Retrofit发送的(Retrofit是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;

    }
 }

我已经使用邮递员试图找出并解决问题,我已将其缩小到$teamMemberPresent变量失败,说明当它作为假的交错时它是空的,但我还没有找到原因。正如我之前提到的,当它设置为true时,查询工作正常。

任何指导都会非常感激。

2 个答案:

答案 0 :(得分:2)

updatePresent()函数中,此代码无法将数据识别为有效,并跳过数据库更新:

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

原因是如果empty($teamMemberPresent)的参数值为false,则isset($teamMemberPresent)返回true。

http://php.net/empty说:

  

如果变量不存在或者其值等于FALSE,则该变量被视为空。

您应该使用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

输出:

lunch

答案 1 :(得分:0)

将你的布尔变量转换为int。

if (index >= 0 && index < list.size()) {
    ...
}