我正在使用表單將值發佈到我的PHP類。檢查變量是否爲0而不是空
<?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;
}
?>
而且功能class_handler
IM檢查是否爲$this->coins
參數的值使用isset function
可悲的功能將始終返回錯誤信息櫃面我會把一個作爲值爲空內。
<?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;
}
}
}
?>
我怎麼能管理,以檢查是否$this->coins
變量不是空的,但可能包含INT 0?
==也做了這個把戲,雖然===不錯,它也會檢查數據類型.. :) – Arshi
@Ashhi'=='return True也是變量是空的或者不存在。 – fusion3k