0

I'm using a form to post values to my PHP class.

<?php
    if(!empty($_POST)){
        require_once('../../handling/admin_add_user.php');

        $add_username = $_POST['add_username'];
        $add_email = $_POST['add_email'];
        $add_gender = $_POST['add_gender'];
        $add_server = $_POST['add_server'];
        $add_coins = $_POST['add_coins'];

        $user_add = new admin_add_user($add_username, $add_email, $add_gender, $add_server, $add_coins);
        $user_add_response = $user_add->class_handler();
        echo $user_add_response;
    }
?>

And inside the function class_handler im checking if the value for the $this->coins parameter is empty using the isset function sadly the function would always return the error message incase i would put a 0 as value.

<?php
    class admin_add_user extends database {
        function __construct($username, $email, $gender, $server, $coins){
            $this->username = $username;
            $this->email = $email;
            $this->gender = $gender;
            $this->server = $server;
            $this->coins = $coins;
        }

        function add_user(){
            $this->connect();
            $this->execute_query("INSERT INTO Users (username, email, gender, server, active, activate_key, coins) VALUES ('" . $this->username . "', '" . $this->email . "', '" . $this->gender . "', '" . $this->server . "', 1, 0, " . $this->coins . ")");
        }

        function class_handler(){
            if(!$this->username){
                return 'Please enter a username.';
            }else if(!$this->email){
                return 'Please enter a email.';
            }else if(!$this->gender){
                return 'Please select a gender.';
            }else if(!$this->server){
                return 'Please select a server.';
            }else if(isset($this->coins)){
                return 'Please enter a coin value. eG: 0';
            }else{
                $this->add_user();
                return 'Succesfull added the following account to the database: ' . $this->username;
            }
        }
    }
?>

How could I manage to check if the $this->coins variable is NOT empty but could contain the int 0?

4 Answers 4

2

I think this should Work:

(isset($this->coins) && $this->coins == 0)

If not then Kindly post your Full Error Message

Sign up to request clarification or add additional context in comments.

Comments

0

To check if a variable is set you can use:

isset( $this->coins );

that return True also if the variable is set to 0;

To check if the variable is set to zero, you have to use:

$this->coins === 0;

2 Comments

== also done the trick, though === is good, it will also check the Datatype.. :)
@Arshi ` == ` return True also is variable is empty or doesn't exists.
0

Since the value is coming from POST it is always a string. You should do something like:

(isset($this->coins) && (!empty($this->coins) || $this->coins === '0')) 

empty() will not work in this case. Because '0' still means empty.

Comments

0

Use !empty in this case:-

!empty($this->coins) //true if $this->coins is neither 0, empty, or set at all';

Is enough.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.