如果用户使用 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;
}
?>
然后我尝试使用 postman 的API,正文看起来像这样
[{
"huffid":"新用户",
"多布":"121212",
"性别":"男",
"国家":"新加坡",
"城市":"新加坡",
“照片网址”:“”
}]
从前端,如果用户没有提供任何照片,photourl
将会为空,就像我使用 postman 发送的一样。数据库列中的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。
关于PHP Mysql,如果用户提交空则指定默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50808146/