【问题标题】:Validation of not allowed username string in input form验证输入表单中不允许的用户名字符串
【发布时间】:2014-11-10 10:55:49
【问题描述】:

我有一个存储在数据库表中的字符串集合,用逗号分隔。

表“not_allowed”内容:

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

在用户注册中,我希望字符串集合“不允许”作为他们的用户名输入。

如何在php中对数据库表格内容进行验证?

我有手动 scipt:

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

if (in_array($username, $notallowed, true)) {
    $errors[] = 'The Selected username is not allowed! ';
}

【问题讨论】:

  • 你的脚本有什么问题?
  • @MarkM,我不想在脚本中输入所有的“字符串”,我需要从数据库表中获取它,以便管理员可以编辑内容。

标签: php mysql


【解决方案1】:

在数据库中创建一个名为not_allowed 的表。使用它来存储所有不允许的字符串。当用户登录时,查询该表以获取所有不允许的字符串。将结果存储在一个变量中,并以与您现在使用它相同的方式使用它。

根据您的程序结构,这可以通过不同的方式完成,但这里是使用 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! ';
}

【讨论】:

  • 不错的答案,我只是没有意识到 fetch_array() 也是一个函数。我真的很愚蠢。
  • 公共函数 SelectNotAllowed() { $query = $this->db->prepare("Select not_allowed FROM website_setting` "); { $查询->执行();返回 $rows = $query->FETCH_ARRAY(); } catch (PDOException $e){die($e->getMessage());} } 不工作!
  • 致命错误:调用未定义的方法 PDOStatement::FETCH_ARRAY()
  • @user3706926 看起来您正在使用 PDO。等效函数为fetchAll()
猜你喜欢
  • 2017-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-19
  • 1970-01-01
  • 2016-06-12
  • 1970-01-01
  • 2011-08-31
相关资源
最近更新 更多