Jump to content

php/mysql tags


herghost

Recommended Posts

Hi Guys

 

Whats the easiest way to store tags in a database from a php form?

 

If I have a text field called tags, and each tag is comma seperated how do I process the data?

 

I have a table called tags, with 3 fields, tag_id, post_id and tag

 

Basically how would I basically turn this input:

 

a tag, b tag, c tag from a form field to

tag_id

cat_id

tag

1

dont worry about me, i can work this out!

tag a

2

dont worry about me, i can work this out!

tag b

3

dont worry about me, i can work this out!

tag c

 

Many Thanks :)

 

Link to comment
https://forums.phpfreaks.com/topic/234446-phpmysql-tags/
Share on other sites

If I have a text field called tags, and each tag is comma seperated how do I process the data?

Use explode. Example

$tags = explode(',', $_POST['your_tag_field'];
print_r($tags);

 

To insert the tags into your tags table I'd do something like this

$cat_id = (int) $cat_id;

$query = 'INSERT INTO tags (cat_id, tag) VALUES (';
// dynamically build the query
foreach($tags as $tag)
{
    $tag = mysql_real_escape_string($tag);
    $query .= "($cat_id, '$tag'), ";
}

// clean the query up, remove the comma and space at the end of the query string
$query = substr($query, 0, -2);

mysql_query($query);

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/234446-phpmysql-tags/#findComment-1204892
Share on other sites

Thanks,

 

I now have this:

$post_id = '1';

$tags = explode(',', $_POST['tags']);
print_r($tags);
$query = 'INSERT INTO blog_tags (post_id, tag) VALUES
(';
// dynamically build the query
foreach($tags as $tag)
	{
    		$tag = mysql_real_escape_string($tag);
    		$query .= "('$post_id', '$tag'), ";
	}

// clean the query up, remove the comma and space at the end of the query string
	$query = substr($query, 0, -2);


mysql_query($query);
if(!$query)
{
echo mysql_error();
}

else
{
echo "ok";
}

 

However nothing is entered into the db, the query prints the OK echo, and if I remove the else the printf works as well?

 

Cheers

 

Link to comment
https://forums.phpfreaks.com/topic/234446-phpmysql-tags/#findComment-1204902
Share on other sites

If you didn't have the parenthesis it wouldn't be a valid query

 

Please stop answering if you dont know what you are going on about

 

WildTeen & Pikachu:

 

Thanks, its now working using:

 


$post_id = '1';

$tags = explode(',', $_POST['tags']);
print_r($tags);
$query = 'INSERT INTO blog_tags (post_id, tags) VALUES
';
// dynamically build the query
foreach($tags as $tag)
	{
    		$tag = mysql_real_escape_string($tag);
    		$query .= "('$post_id', '$tag'), ";
	}

// clean the query up, remove the comma and space at the end of the query string
	$query = substr($query, 0, -2);

$go=mysql_query($query);
if(!$go)
{
echo mysql_error();

}

else
{
echo "ok";
echo '<br>';
echo $go;
}

Link to comment
https://forums.phpfreaks.com/topic/234446-phpmysql-tags/#findComment-1204916
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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