Jump to content

search names and numbers syntax


dflow

Recommended Posts

i have this code

i am doing something wrong, i want only letters in the  Name search

$query = 'SELECT *
FROM apartments
INNER JOIN suppliers ON apartments . InternalSupplierID = suppliers . InternalSupplierID
WHERE ID LIKE '.$_POST['search'].'
OR SupplierID LIKE '.$_POST['search'].'
OR Name LIKE \' {^[[:alpha:]]+$}'.$_POST['search'].' {^[[:alpha:]]+$}\'';

Link to comment
https://forums.phpfreaks.com/topic/228483-search-names-and-numbers-syntax/
Share on other sites

Would you totally be against PHP preg_replace?

 

If not then you can use this

  
  $searchTerm = preg_replace('/[^a-zA-Z]/','',$_POST['search']);

  $query = "SELECT *
  FROM apartments
  INNER JOIN suppliers ON apartments . InternalSupplierID = suppliers . InternalSupplierID
  WHERE ID LIKE '{$_POST['search']}'
  OR SupplierID LIKE '{$_POST['search']}'
  OR Name LIKE '%$searchTerm%'";

 

Regards, PaulRyan.

OR Name LIKE \' {^[[:alpha:]]+$}'.$_POST['search'].' {^[[:alpha:]]+$}\'';

What is this supposed to do exactly?

 

You would be much better off using a preg_replace on your $_POST input to strip out anything that you want there (as PaulRyan has shown), and then enter it into the query. SQL isn't neerly as good at that type of data manipulation as PHP is.

Would you totally be against PHP preg_replace?

 

If not then you can use this

  
  $searchTerm = preg_replace('/[^a-zA-Z]/','',$_POST['search']);

  $query = "SELECT *
  FROM apartments
  INNER JOIN suppliers ON apartments . InternalSupplierID = suppliers . InternalSupplierID
  WHERE ID LIKE '{$_POST['search']}'
  OR SupplierID LIKE '{$_POST['search']}'
  OR Name LIKE '%$searchTerm%'";

 

Regards, PaulRyan.

 

search for Name still doesnt work :(

Do you get any errors? If so what are they?

 

If not, are you searching for something that actually exists?

Try changing the other variables to %$searchTerm% too?

 

If non of the above is the case try allowing spaces with the following

  $searchTerm =  preg_replace('/[^a-zA-Z\s]/','',$_POST['search']); 

 

Regards, PaulRyan.

Do you get any errors? If so what are they?

 

If not, are you searching for something that actually exists?

Try changing the other variables to %$searchTerm% too?

 

If non of the above is the case try allowing spaces with the following

  $searchTerm =  preg_replace('/[^a-zA-Z\s]/','',$_POST['search']); 

 

Regards, PaulRyan.

perfect thanks

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.