0

I have a collection of string stored in database table separated by comma.

Table "not_allowed" content :

admin,administrator,register,email,password,username,database,computer,... etc. (as admin defind)

In user registration, I want that collection of string is "not allowed" as their username input.

How to make the validation in php in relation with database table content?

Manual scipt I have :

$notallowed = array('admin','administrator','register','email','password','username','database','computer',... etc);

if (in_array($username, $notallowed, true)) {
    $errors[] = 'The Selected username is not allowed! ';
}
2
  • What's not working with the script you have? Commented Nov 10, 2014 at 2:40
  • @MarkM, I don't want to type all the 'string' in the script, I need to get it from database table that the content can be edited by administrator. Commented Nov 10, 2014 at 2:47

1 Answer 1

1

Create a table in the DB called not_allowed. Use it to store all the not allowed strings. When a user signs in, query that table to get all the not allowed strings. Store the result in a variable and use it in the same way you use it now.

Depending on your program structure this can be done in different ways, but here's the basic idea using mysqli:

$query = $mysqli->query("SELECT * FROM not_allowed");
$not_allowed = $query->fetch_array();

if (in_array($username, $not_allowed, true)) {
    $errors[] = 'The Selected username is not allowed! ';
}
Sign up to request clarification or add additional context in comments.

4 Comments

Nice answer, I just don't realize that fetch_array() is a function too.. I am really stupid.
public function SelectNotAllowed() { $query = $this->db->prepare("Select not_allowed FROM website_setting` "); { $query->execute(); return $rows = $query->FETCH_ARRAY(); } catch (PDOException $e){die($e->getMessage());} } is not working!
Fatal error: Call to undefined method PDOStatement::FETCH_ARRAY()
@user3706926 Looks like you are using PDO. The equivalent function is fetchAll()

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.