简体   繁体   English

如果一个输入值大于php中的另一个输入值,则回显错误消息

[英]Echo error message if one input value is greater than other in php

I have a form that saves 5 input values to an inventory in data base. 我有一个将5个输入值保存到数据库清单中的表格。 The problem is, i need it to return an error message if "Value" is larger than "Buy_Value". 问题是,如果“值”大于“ Buy_Value”,我需要它返回错误消息。 Right now it prints the error message, but still submits the data to data base which is no use for me. 现在,它会打印错误消息,但仍将数据提交给数据库,这对我没有用。 Am I just using the function in the wrong place in the code? 我只是在错误的代码位置使用了该函数吗?

 if($_SERVER["REQUEST_METHOD"] == "POST"){
if(isset($_POST['ID'])&&isset($_POST['Name'])&&isset($_POST['Inventory_number'])
&&isset($_POST['Value'])&&isset($_POST['Buy_Value'])){

            $ID=$_POST['ID']; 
    $Name=$_POST['Name']; 
    $Inventory_number=$_POST['Inventory_number'];
    $Value=$_POST['Value'];
    $Buy_Value=$_POST['Buy_Value'];
    //echo "<p>".$ID." ".$Name." ".$Inventory_number."</p>";
    include('config.php');

    if ($Value > $Buy_Value) {
echo("The 'buy value' must be greater than 'value'");
}
$sql="INSERT INTO $mysql_database.`inventory` 
(`id`, `Name`, `Inv_Nr`, `value`, `buy_value`) 
VALUES ('$ID', '$Name', '$Inventory_number', '$Value', '$Buy_Value')";
$result=mysql_query($sql);
tab($sql);
mysql_close($bd);
    }
 }

     function tab($sql){
if (!mysql_errno())
{
    echo "<br />Data submitted";
}
    else
{
    echo "<br />Error: " . mysql_error();
}
}
 ?>

put your database query execution in else part then it will not executed if below 将您的数据库查询执行放在其他部分,则在以下情况下将不执行

  if ($Value > $Buy_Value) {

condition is meet. 条件满足。

try this: 尝试这个:

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (isset($_POST['ID']) && isset($_POST['Name']) && isset($_POST['Inventory_number'])
        && isset($_POST['Value']) && isset($_POST['Buy_Value'])
    ) {

        $ID = $_POST['ID'];
        $Name = $_POST['Name'];
        $Inventory_number = $_POST['Inventory_number'];
        $Value = $_POST['Value'];
        $Buy_Value = $_POST['Buy_Value'];
        //echo "<p>".$ID." ".$Name." ".$Inventory_number."</p>";
        include('config.php');

        if ($Value > $Buy_Value) {
            echo("The 'buy value' must be greater than 'value'");
        } else {
            $sql = "INSERT INTO $mysql_database.`inventory`
(`id`, `Name`, `Inv_Nr`, `value`, `buy_value`)
VALUES ('$ID', '$Name', '$Inventory_number', '$Value', '$Buy_Value')";
            $result = mysql_query($sql);
            tab($sql);
            mysql_close($bd);
        }
    }
}

function tab($sql)
{
    if (!mysql_errno()) {
        echo "<br />Data submitted";
    } else {
        echo "<br />Error: " . mysql_error();
    }
}

在下一行使用exit而不是echo

exit("The 'buy value' must be greater than 'value'");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM