Home > database >  transfer several parameters from a form into SQL query
transfer several parameters from a form into SQL query

Time:12-20

i am working on a table that includes a filter function. For the filter i use a form where i enter the parameters. Those are added to a string which is my SQL query. So far it works fine.

There is oine input field where multiple parameters canbe added. The plan is to seperate them with ; . For example 520;521;522

My plan was to use str_replace to convert this in to sql Code.

For example

$str = str_replace(";", "" OR ", "520;521;522");

Results in to:

SELECT * FROM MaschinenVorgangslisteMitHV WHERE (VorgangNr LIKE 'R0%' or 'R2%' or 'R3%')

But some how this code does not show the expected results. I only get results for 'R0%'

How do i need to adjust this query in order to have the sql query working?

$str = str_replace(";", "" OR ", "520;521;522");

Results in to:

SELECT * FROM MaschinenVorgangslisteMitHV WHERE (VorgangNr LIKE 'R0%' or 'R2%' or 'R3%')

In another input field i search for names. Here the query looks like this... SELECT * FROM MaschinenVorgangslisteMitHV WHERE (Bearbeiter LIKE '%Heine%' OR Bearbeiter LIKE '%Wolf%' OR Bearbeiter LIKE '%Maiwald%') This works fine!

CodePudding user response:

The multiple like should be written as,

SELECT *  
FROM MaschinenVorgangslisteMitHV  
WHERE VorgangNr REGEXP '520|522|523';

CodePudding user response:

I believe you need to add VorgangNr LIKE after every OR.

  • Related