如果用户使用 php 和 mysql 查询提交空值(如[{"photourl":""}]
(,我正在尝试给出默认值。我不太熟悉后端,所以我不确定我的代码是否正确。
<?php
function profile_put($dt)
{
$query="update personal set fullname='".$dt[0]["name"]."', dob='".$dt[0]["dob"]."',
gender='".$dt[0]["gender"]."', country='".$dt[0]["country"]."', city='".$dt[0]["city"]."', photourl='".$dt[0]["photourl"]."',
lastupdate='15 minutes', about='', website=''
where huffid='".$dt[0]["huffid"]."';";
if($dt[0]["photourl"]=="")
{
$dt[0]["photourl"]='https://i.imgur.com/KbHbcqV.png';
}
return $query;
}
?>
然后我使用邮递员尝试了 API,身体看起来像这样[{
"huffid":"newuser",
"dob":"121212",
"gender":"male",
"country":"singapore",
"city":"singapore",
"photourl": ""
}]
从前端,如果用户不提供任何照片,photourl
将清空,就像我使用邮递员发送的照片一样。数据库列中的photourl
也很感动。请问谁能帮忙?
您需要先分配默认值,然后再将其放入$query
中。语句按顺序执行,在赋值之前不能引用该值。
<?php
function profile_put($dt)
{
if($dt[0]["photourl"]=="")
{
$dt[0]["photourl"]='https://i.imgur.com/KbHbcqV.png';
}
$query="update personal set fullname='".$dt[0]["name"]."', dob='".$dt[0]["dob"]."',
gender='".$dt[0]["gender"]."', country='".$dt[0]["country"]."', city='".$dt[0]["city"]."', photourl='".$dt[0]["photourl"]."',
lastupdate='15 minutes', about='', website=''
where huffid='".$dt[0]["huffid"]."';";
return $query;
}
?>
您还应该学习使用准备好的查询,而不是将字符串替换为 SQL。