Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 错误的布尔值语句未插入数据库_Php_Mysql_Prepared Statement - Fatal编程技术网

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

Php 错误的布尔值语句未插入数据库,php,mysql,prepared-statement,Php,Mysql,Prepared Statement,我有一个JSON数组,如下所示: { "operation": "update_original_team_member", "teamMember": { "teamMemberPresent": false, "unique_id": "5a5b7f6b835408.50059003", "user_unique_id": "59bea8b7d56a63.33388595" } } 我试图将其插入mySQL数据库,但每

我有一个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
变量failing,指出当它作为false相互关联时它是空的,但我不知道为什么。如前所述,当其设置为true时,查询工作正常


任何指导都将不胜感激

将布尔变量强制转换为int

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

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

if (!empty ($teamMemberPresent)&& !empty ($unique_id)&& !empty ($user_unique_id)){
 ... 
原因是
empty($teamMemberPresent)
如果其参数具有
false
值,则返回true

说:

如果变量不存在或其值等于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

调用函数时,
$teamMemberPresent
在函数中的值是什么?如何调用
updatePresent
,以及向其传递什么值?如果没有这些信息,你的问题就无法回答。这些值都在JSON数组中(在我问题的开头),我刚刚在你建议的部分添加了
isset
,它解决了我的问题!我认为,因为在传递到函数updatePresent之前,我检查了
isset
,所以没有必要再次检查它,但显然是这样。谢谢你的建议,我现在就接受你的答复。