dflow Posted February 22, 2011 Share Posted February 22, 2011 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:]]+$}\''; Quote Link to comment https://forums.phpfreaks.com/topic/228483-search-names-and-numbers-syntax/ Share on other sites More sharing options...
PaulRyan Posted February 22, 2011 Share Posted February 22, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/228483-search-names-and-numbers-syntax/#findComment-1178119 Share on other sites More sharing options...
Muddy_Funster Posted February 22, 2011 Share Posted February 22, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/228483-search-names-and-numbers-syntax/#findComment-1178122 Share on other sites More sharing options...
dflow Posted February 22, 2011 Author Share Posted February 22, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/228483-search-names-and-numbers-syntax/#findComment-1178125 Share on other sites More sharing options...
Muddy_Funster Posted February 22, 2011 Share Posted February 22, 2011 what's it not doing / doing that it shouldnt? Quote Link to comment https://forums.phpfreaks.com/topic/228483-search-names-and-numbers-syntax/#findComment-1178127 Share on other sites More sharing options...
PaulRyan Posted February 22, 2011 Share Posted February 22, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/228483-search-names-and-numbers-syntax/#findComment-1178129 Share on other sites More sharing options...
dflow Posted February 22, 2011 Author Share Posted February 22, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/228483-search-names-and-numbers-syntax/#findComment-1178130 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.