Question

I have a form that saves 5 input values to an inventory in data base. The problem is, i need it to return an error message if "Value" is larger than "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();
}
}
 ?>
Was it helpful?

Solution

Use exit instead of echo for the following line

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

OTHER TIPS

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();
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top