我尝试在将数字数据添加到数据库之前验证数字数据,但由于某种原因,它不会在正逻辑或负逻辑中触发错误消息的条件。我可以放入字母和特殊字符,代码仍然没有捕获错误。我已经阅读了许多其他类似的问题,这些问题似乎都是语法错误,但我找不到代码的问题。
<?php
$errorMsg = ""; // error messages variable
if(is_numeric($Shear)) {
$Shear = $_POST['Shear'];
}
else if(!($Shear)) {
$errorMsg = "You must enter level for Shear Hydraulics.";
}
else {
$errorMsg = "You must enter numeric data for Shear Hydraulics.";
}
if (!empty($_POST['High'])){ // check if level was entered
$High = $_POST['High'];
}
else if(!is_numeric($High)) { //check if only numbers were used
$errorMsg = "You must enter numeric data for High Pressure Hydraulic."; // and if not - add error message
}
else{
$errorMsg = "You must enter level for High Pressure Hydraulic."; // and if not - add error message
}
if (!empty($errorMsg)){ // if there are errors, display them.
echo ' <script type="text/javascript">
alert("'.$errorMsg.'");
</script>
';
} ?>
这是我尝试过的两种方法,都没有捕获非数字数据。