Solved

Tag Functionality

Posted on 2013-10-25
1
375 Views
Last Modified: 2013-10-25
I have an html form that I want to also have an input field for a user to add tags separated by commas such as this form here on experts-exchange.

I'm not sure how to insert it into the database though as I would want one tag per row in the database not one row with all the tags still separated by commas. Is there a better way to do this?
0
Comment
Question by:N R
1 Comment
 
LVL 43

Accepted Solution

by:
Chris Stanyon earned 500 total points
ID: 39601752
Have a look through this:

//connect to your database
$conn = new PDO('mysql:host=localhost;dbname=yourDB', 'username', 'password');

//create your INSERT query string with a named parameter
$sqlStr = "INSERT INTO tagsTable (tagColumn) VALUES (:tag)";

//prepare the query
$InsertTags = $conn->prepare($sqlStr);

//split the POST['tags'] value on a comma
$tags = explode(",", $_POST['tags']);

//loop through the tags
foreach ($tags as $tag) {
	//insert each tag into the database
	$InsertTags->execute(array('tag'=> trim($tag)));
}

Open in new window

It assumes you pass your tags through the POST['tags'] field. Any questions, ask away
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question