Solved

Tag Functionality

Posted on 2013-10-25
1
369 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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

864 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now