svgmx5 Posted June 23, 2011 Share Posted June 23, 2011 So here's what i'm trying to do... what i'm trying to do is to be able to type in several keywords in a text field...ie: red, black, blue (seperated by ',') Upon submitting the form then i want to look for each one of those keywords in the database and get their information. I have the following code, the problem with the code below is that it only returns one value. What i want it to do is to return all the values, so if a user searches for black, blue, white it then looks for those three values, grabs their ID and displays each ID. My end result is actually to store them in another table. I hope someone can help me out figure this bug out. <?php if(isset($_POST['submit'])){ $tagNames = $_POST['tags']; $tags = explode(',' ,$tagNames); //seperates the values that have a ',' at the end. $pointTagArr = array();//creates a new array $i = 0; foreach($tags as $x=> $y){ //looks through the $tags and places them in the new $pointTagArr, array $pointTagArr[] = $tags[$x]; $i++; } $getPonitTagId = mysql_query("SELECT * FROM tags WHERE tag_name IN('".implode("','", $pointTagArr)."')") or die(mysql_error()); //this is the query that's supposed to look for each value by using the implode function while($pointID = mysql_fetch_assoc($getPonitTagId)){ //after it finds the values, it's supposed to look through them and display them, or add them to another table echo $pointID['id']; } } ?> <form method="post" action="test2.php"> <input type="text" name="tags" /> <br/> <input type="submit" name="submit" value="Submit" /> </form> Quote Link to comment Share on other sites More sharing options...
svgmx5 Posted June 23, 2011 Author Share Posted June 23, 2011 okay, so figured out what the problem was....it seems that when i type in the values in the text field i have to keep them all close ie: red,black,blue it seems that i can't have any spaces ie: red, black, blue Now the questions is how i can i remove the white space if someone is to add a space after each ',' Quote Link to comment Share on other sites More sharing options...
Alex Posted June 23, 2011 Share Posted June 23, 2011 You have a lot of superfluous code there. To solve your issue with the spaces you can use trim $tags = array_map('mysql_real_escape_string', array_map('trim', explode(',', $_POST['tags']))); $getPonitTagId = mysql_query("SELECT * FROM tags WHERE tag_name IN('".implode("','", $tags)."')") or die(mysql_error()); I also added some security using mysql_real_escape_string array_map 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.