0
votes

I am trying to enable users to change the sale price in game currency for a website I am making. Currently when I change the price to any number >= 1, the change is saved properly as expected. However when trying to set the price back to 0, which is the default value as well, the change is not saved.

In the SQL database the attribute for the sell price is set to an int(10).

When the user clicks the save options button, the sale fee is first set up as a variable with the previous value of the sale price like so:

$salePrice = $cPet[17];

Then this code is run:

    if(!empty($_POST['sellFee'])) { # if sell fee included
            if ($_POST['sellFee'] < 0) {
                echo "Sale fee must be a positive number.";
            } else if ($_POST['sellFee'] >= 0){
                $salePrice = $_POST['sellFee'];
                echo "sale fee is 0 or above";
            } else {
                echo "Something went wrong with the sale fee.";
            }

            
        }

So if the sell fee has been included in the form, the program first catches negative numbers. Then if the number is >= 0 it should set the sale fee to that number. This function works perfectly for any number that is not 0.

No errors are shown. "Something went wrong with the sale fee." never appears. When entering 0 as the price, "sale fee is 0 or above" does not appear but neither does the warning about something going wrong. I don't understand why it's not catching it with 0 in the >= 0 statement.

I tried changing the data type to a VARCHAR to see what would happen. It let me save it to 00, but not 0. I also tried removing the default value but the 0 price is still not saved.

Please note that 0 is regarded as empty in PHP . Hence if(!empty($_POST['sellFee'])) will never get executed in your problematic case. Please see this linkKen Lee
Change if(!empty($_POST['sellFee'])) to if(isset($_POST['sellFee']))Kevin Y