简体   繁体   English

检查区分大小写条目的SQL条目的更好方法

[英]Better Way to Check SQL entries for Case-Sensitive Entries

Background 背景

I have a web program which has an administrator panel that allows the current logged in user to add new logins and passwords into the system for other users. 我有一个带有管理员面板的Web程序,该面板允许当前登录的用户为其他用户向系统添加新的登录名和密码。 While working on some code, it had occurred to me that although I check for the same 'first' and 'last' names, as well as same 'username' in the system, I do not currently check for the user 'Bilbo Baggins' as should 'bilbo baggins' be checked. 在处理某些代码时,我想到,尽管我检查系统中相同的“名”和“姓”以及相同的“用户名”,但我目前不检查用户“ Bilbo Baggins”以及应检查“ bilbo baggins”。

In theory, I could do two times the checking of all similarities... whether he be 'bilbo baggins', 'bILBO BaggINS' or 'BILBO BAGGINS'... But I presume there would be a better way to do this rather than rewriting the same code to check over and over the same information. 从理论上讲,我可以对所有相似之处进行两次检查...他是“ bilbo baggins”,“ bILBO BaggINS”还是“ BILBO BAGGINS” ...但我想这样做会比这更好。重写相同的代码以检查相同的信息。

Using my current implementation, does someone have a similar situation to check 1) the same first name and last name are not reserved in the system, and 2) the same username does not exist? 使用我当前的实现,有人是否有类似情况要检查1)系统中没有保留相同的名字和姓氏,以及2)不存在相同的用户名?

CODE: 码:

...set all variables used in post...

try {
    //Confirm that there are no other same first and last names with the same registration
    $query = $general->db->prepare("SELECT * FROM users WHERE firstname = :firstname AND lastname = :lastname");
    $names  =  array(
      'firstname' => $firstname,
      'lastname' => $lastname);
  $query->execute($names);
  $result = $query->fetchAll();

      if (sizeof($result) == 0) {
          //Now check that the username isn't currently in use
          $query = $general->db->prepare('SELECT * FROM users WHERE username = :username');
          $username = array('username' => $username);
          $query->execute($username);
          $usernames_exist = $query->fetchAll();

          if (sizeof($usernames_exist) == 0) {
          ##both fullname test as well as username check passed, so allow user to enter the information
            $addedUser = $users->register_user($firstname, $lastname, $username['username'], $password, $cellphone, $seclvl, $email, $direct, $extension);
            $message = "Congratulations, you have successfully added **AN ALREADY ACTIVATED** user with the following information: <br><br>";   

MySQL WHERE clauses are case insensitive by default. MySQL WHERE子句默认情况下不区分大小写。 So... 所以...

  • Bilbo_Baggins Bilbo_Baggins
  • bilbo_baggins bilbo_baggins
  • bILBO_BaggINS bILBO_BaggINS
  • BILBO_BAGGINS BILBO_BAGGINS

...are all only going to find the one record containing 'bilbo_baggins' in the db. ...只会在数据库中找到一条包含“ bilbo_baggins”的记录。 Just make sure that field has a UNIQUE index. 只要确保该字段具有唯一索引即可。

See here for more info. 有关更多信息,请参见此处

For your system to function well you need to not check on first / last name. 为了使系统正常运行,您无需检查名字/姓氏。 But now to get back on point. 但是现在回到正题。

What you can do is have all of the text in lower case form it enters the database. 您可以做的是将所有小写形式的文本输入数据库。 Once you pull it out you can format the text into the correct form. 一旦将其拉出,就可以将文本设置为正确的格式。

This allows all of your data to function in the same way so you don't have to have so many checks. 这样一来,您所有的数据都能以相同的方式运行,因此您不必进行太多检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM