I am trying to restrict the value entered for the form field 'active' to either 0 or 1, 0 being inactive and 1 being active
The code that is inserting the user in the database:
if ($_POST['active'] != '1' || $_POST['active'] != '0') {
$errors[] = 'Active field can only take values of either 1 or 0';
} else {
if (empty($_POST) === false && empty($errors) === true) {
$register_data = array(
'username' => $_POST['username'],
'password' => $_POST['password'],
'first_name' => $_POST['first_name'],
'email' => $_POST['email'],
'active' => $_POST['active'],
);
admin_add_user($register_data);
header('Location: add_user.php?success');
exit();
}
else if (empty($errors) === false) {
echo output_errors($errors);
}
}
The function:
function admin_add_user($register_data) {
include('core/db/db_connection.php');
array_walk($register_data, 'array_sanitize');
$register_data['password'] = md5($register_data['password']);
$fields = '`' . implode('`, `', array_keys($register_data)) . '`';
$data = '\'' . implode('\', \'', $register_data) . '\'';
$sql = "INSERT INTO `_users` ($fields) VALUES ($data)";
$query = mysqli_query($dbCon, $sql);
}
How can I restrict the values entered for the form field named 'active' to 1 or 0? I have tried 1 and 0 without the apostrophes but I still get the error associated with this if statement.