Solved

Tag Functionality

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Creating and Managing Databases with phpMyAdmin in cPanel.
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 look for a specific file type in a local or remote server directory using PHP.

747 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

9 Experts available now in Live!

Get 1:1 Help Now