我正在尝试创建一个if-else语句,即使所需的输入场是空的,它也不会将任何信息插入数据库
我尝试使用=!并且==但无济于事,我似乎无法想到另一种方法来获得我需要的条件陈述。这是我尝试做的:
public InputField inputUserName;
public InputField inputEmail;
string CreateUserURL = "http://localhost/balikaral/insertAccount.php";
public void verif()
{
if (inputUserName != "" && inputEmail != "")
{
CreateUser(); //method which contains the function to insert the inputted data into the database
}
else
{
print("error");
}
}
首先,您正在检查 InputField
(not(等于'''。输入场是一个对象,永远不会是字符串值。您想要InputField.text
。
我还发现,将我的条件分开为单个陈述并附加到一个错误,以便调试器/用户对问题有清晰的了解。然后,您也可以通过这种方式将错误发布到对话框中。尝试以下内容:
public void verif()
{
StringBuilder errorBuilder = new StringBuilder();
if (string.IsNullOrWhiteSpace(inputUserName.text))
{
errorBuilder.AppendLine("UserName cannot be empty!");
}
if (string.IsNullOrWhiteSpace(inputEmail.text))
{
errorBuilder.AppendLine("Email cannot be empty!");
}
// Add some more validation if you want, for instance you could also add name length or validate if the email is in correct format
if (errorBuilder.Length > 0)
{
print(errorBuilder.ToString());
return;
}
else // no errors
{
CreateUser();
}
}