Solved

Tag Functionality

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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 dynamically set the form action using jQuery.

726 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