Jump to content

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

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.