I’m working on a gig right now where the client wants the user to be able to search for a product by product code.
A product code is formatted like so: 123.4567.89
So, the search box should return that product whether the user enters the number with the periods, without the periods, or with spaces.
So, all of the following should return the product: 123.4567.89, 123456789, 123 4567 89.
My current query looks like so:
SELECT *
FROM products
WHERE product_code LIKE '%$search_code%'"
I’m at a loss as to how I would revise that to include all the different possibilities of how a user would input these numbers.
Thanks in advance for any help.
[Front End] Limit the characters the user can enter. Only allow periods and spaces. Don’t allow any alpha characters (if all your product SKUs are numerical).
[Middle Tier] After the form is posted, double check the data for extraneous characters on the back end. If somehow the client managed to bipass the validation on the front end, you can catch it on the back end. Use a simple search and replace in your language of choice.
[Database/Back-End] Once the data is restricted to only numeric digits and you send the SKU to your database query, strip out all periods on your products table. If you know you only use periods to store the SKUs, just search excluding them, e.g.
Avoid wildcard %% searches, they’re expensive.