case sensitive PHP

Asked

Viewed 268 times

0

I want a variable that is passed by POST to be case-insensitive, ie if for example the variable is "Test", be equal to "test". I know that there is in PHP strcasecmp, which does this. The problem is that I do a search in the table (Sqlite database) of this value passed by POST. In the table there is for example a field with this value Test, and if it is passed test, know that it is this field. here is my query the table:

$requete="SELECT * FROM contact WHERE nom='$search'";

and the value

if (isset($_POST['search'])) 
{
    $search= htmlentities($_POST['search']);
}
  • It is important that accentuation is ignored?

  • in PHP or Sqlite?

  • solved using ucwords($search);

1 answer

6

If the problem is in query:

$requete = "SELECT * FROM contact WHERE LOWER( nom ) = LOWER( '$search' )";

or else:

$search = mb_strtolower( $search );
$requete = "SELECT * FROM contact WHERE LOWER( nom ) = '$search'";

Or equivalent function, because the DBMS was not specified in the question. Remembering that in this case it is fundamental to use the collation correct for the language in use in the languages involved.


The SQL Injection comes free in all these codes. Give a searched here at the same Sopt, has a series of answers explaining how to solve. Regardless of the original question, your code sooner or later will give you a serious problem with this


If the problem is in the name of the parameters:

Normally this should not be necessary, and should only be used if there is a conscious reason for it. In general, it’s best to use coherence when naming everything in PHP (and any other language). Looking at the current situation of the question, I believe that the first part of the answer is the solution, not this.


With this line we normalize the marry of the parameters:

$_POST_MIN = array_change_key_case( $_POST, CASE_LOWER );

And then use it this way, always in lowercase:

if (isset($_POST_MIN['search'])) 
{
    $search= htmlentities($_POST_MIN['search']);
}
  • +1 The problem is not even in PHP, it is in the collation of the base.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.