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
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.

Link to comment
Share on other sites

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.

Link to comment
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.

 

search for Name still doesnt work :(

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.