CanMan2004 Posted October 16, 2006 Share Posted October 16, 2006 Hi allAm kicking myself tryin to get this to work.Basically I have a database which holds users details, each row have an id number, full users name etc and there is also a field calledareasallowedwithin this field is a bunch of numbers seperated by commas, for example12, 56, 78, 101, 120, 167, 200, 271, 325, 427and that's that.I then have a front end search form which I have a input field calledareanamefieldthe user can enter a number value into, for example23and then perform a search.The current query I use is[code]SELECT * FROM members WHERE areaname = $_GET['areanamefield'][/code]but at the moment, I cant get a result back for a number stored within the commas seperated field, so if I search for.56then it will only return rows which have just56stored in it, if56appears within a commas seperated value, then it wont return it.Can anyone help?I have tried the query[code]SELECT * FROM members WHERE areaname IN ($_GET['areanamefield'])[/code]But it didnt seem to like that.Any help would be greatThanks in advanceDave Quote Link to comment Share on other sites More sharing options...
neoform Posted October 16, 2006 Share Posted October 16, 2006 you're going to want to not store your data like that in your database..each number should be it's own row in a table, not comma delimited. Quote Link to comment Share on other sites More sharing options...
craygo Posted October 16, 2006 Share Posted October 16, 2006 make your search like this[code]SELECT * FROM members WHERE areaname LIKE '%,56 %'[/code]Ray Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted October 16, 2006 Share Posted October 16, 2006 You have the arguments reversed in your query. Try:[code]<?php $q = "SELECT * FROM members WHERE "' . $_GET['areanamefield'] . "' in areasallowed"; ?>[/code]Ken Quote Link to comment Share on other sites More sharing options...
craygo Posted October 16, 2006 Share Posted October 16, 2006 if you are going to search for more than one area seperated by a comma then you can use this. I put a test form together to test. You can change to fit your needs[code]<?phpif(isset($_POST['submit'])){/**** important stuff ****/$areaname = explode(", ", $_POST['area']);foreach($areaname as $key => $val){$sql = "SELECT * FROM members WHERE areasallowed LIKE '%$val%'"; $res = mysql_query($sql) or die (mysql_error()); while($r = mysql_fetch_assoc($res)){ echo $r['name']."--".$r['areasallowed']."<br>"; }}/**** END STUFF ****/} else {print '<form name=test method=POST action="'.$_SERVER['PHP_SELF'].'"> Area: <input type=text name=area><br /> <input type=submit name=submit value=submit>'; }?>[/code]This will take the area's entered, seperated by commas, and divide it up and search each area. Only thing you really need is the area between the important stuff :)Ray Quote Link to comment Share on other sites More sharing options...
CanMan2004 Posted October 16, 2006 Author Share Posted October 16, 2006 thanks everyone Quote Link to comment 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.