Reputation: 11
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?
Upvotes: 0
Views: 152
Reputation: 6539
Use !empty in this case:-
!empty($this->coins) //true if $this->coins is neither 0, empty, or set at all';
Is enough.
Upvotes: 0
Reputation: 4076
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.
Upvotes: 0
Reputation: 511
I think this should Work:
(isset($this->coins) && $this->coins == 0)
If not then Kindly post your Full Error Message
Upvotes: 2
Reputation: 11689
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;
Upvotes: 0