Jump to content

using implode() inside a mysql query


svgmx5

Recommended Posts

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>

Link to comment
Share on other sites

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 ','

Link to comment
Share on other sites

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

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.