我有这个PHP代码:
<?php
$score11 = $_POST['passmarks12'];
if($_POST['passmarks12'] > 100){
$grade11 = "";
}
elseif ($_POST['passmarks12'] < 45){
$grade11 = "Fail";
}
$strg = " $grade11";
echo $strg;
?>
无论发送什么内容,代码始终打印“失败”。
我想要它,如果它传入空白或无效输入,它就会失败。
我应该如何正确地清理输入?
答案 0 :(得分:1)
试试这个:
<?php
//$_POST['passmarks12'] = '';
if(empty($_POST['passmarks12']) || $_POST['passmarks12'] > 100)
{
$grade11 = "";
}
else if ($_POST['passmarks12'] < 45){
$grade11 = "Fail";
} else{
$grade11 = "Pass";
}
$strg = " $grade11" ;
echo $strg;
?>
答案 1 :(得分:1)
$_POST['key']
检查isset
是否存在。$_POST['key']
是否包含有效数据类型字符串
它可能来自数组。$_POST['key']
是否具有有效的数字格式。 $_POST['key']
比较45
(字符串)和intval
(整数)。<?php
switch (true) {
case !isset($_POST['passmarks12']):
case !is_string($score = $_POST['passmarks12']):
case !is_numeric($score):
$result = 'Error (Invalid parameter)';
break;
case (intval($score) < 45):
$result = 'Fail (Less than 45)';
break;
default:
$result = 'Success (No less than 45)';
}
echo $result;
答案 2 :(得分:0)
我想你想要
elseif ($_POST['passmarks12'] < 45 && !empty($_POST['passmarks12']))
答案 3 :(得分:-1)
此PHP代码将确保$_POST
在将其与100比较之前不为空。
<?php
$score11 = $_POST['passmarks12'];
if(empty($_POST['passmarks12']) || $_POST['passmarks12'] > 100)
$grade11 = "";
elseif ($_POST['passmarks12'] < 45)
$grade11 = "Fail";
$strg = " $grade11" ;
echo $strg;
?>