[英]False boolean value statement not inserting to database
我有一個看起來像這樣的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
變量失敗,指出在它為false時它為空,但我不知道為什么。 如前所述,將其設置為true時,查詢工作正常。
任何指導將不勝感激。
在您的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
將您的布爾變量強制轉換為int。
$query->execute(array(':present' => (int) $teamMemberPresent,':unique_id' => $unique_id, ':user_unique_id' => $user_unique_id));
// this one -> (int) $teamMemberPresent
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.