?
Solved

Tag Functionality

Posted on 2013-10-25
1
Medium Priority
?
402 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:Nathan Riley
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 43

Accepted Solution

by:
Chris Stanyon earned 2000 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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
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 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)
Suggested Courses

777 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