Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to insert a string of given tags into a MySQL table, but it's not quite working out.

  $tags = array explode (", ", $_POST["tags"]);
  $tag_array = implode(", ", $tags); 

My table has the columns tag_id(auto increment) and tag_name(where each item of array needs to go into)

How do I cycle through my array and make MySQL insert a blank value into tag_id (as it's auto incremented) and each of my array values into the tag_name column?

share|improve this question
3  
once you implode, you don't have an array anymore. you've got a STRING. and I hope you're using proper sql injection attack defense mechanisms, and not just trying to stuff that string into your DB directly... –  Marc B Jan 18 at 16:36
 
1. I assume MySQL is using tag_id as a primary key, so dont insert a blank value, 2. $mysqli->prepare("INSERT INTO tags VALUES (?)") combined with foreach –  Hiroto Jan 18 at 16:37
 
I also suggest you program some logic to account for if a user enters data like: apple, orange,banana, [three spaces] tomato, [6 spaces] etc.. You can do this easily by trim($tags) and/ or just separating by comma (without the space) like explode(',', $_POST['tags']);. –  user1477388 Jan 18 at 16:38
 
What is the purpose of your code in the example? $tag_array will be equal to $_POST["tags"] afterwards. –  lethal-guitar Jan 18 at 16:38
 
Yeah, sorry. The code is all in very basic stages. It's used to bind tags to photos. I'm not sanitizing the string yet. This is still all on a local server trying to get it all working. I know I still have stuff like that to do. –  Rudy van Sloten Jan 18 at 16:42

2 Answers

up vote 0 down vote accepted

You don't mention how you're accessing MySQL from within PHP, but using PDO you could do something like this:

$dbh = new PDO("mysql:dbname=$dbname", $username, $password);
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, FALSE);

$insert = $dbh->prepare('INSERT INTO my_table (tag_name) VALUES (:tag)');
$insert->bindParam(':tag', $tag, PDO::PARAM_STR);

foreach ($tags as $tag) $insert->execute();
share|improve this answer
 
I'm simply using mysql_query in either a PHP while or foreach –  Rudy van Sloten Jan 18 at 16:43
1  
@RudyvanSloten: mysql_query() (along with the rest of the ancient mysql PHP extension) is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. –  eggyal Jan 18 at 16:46
 
Perfect. I will look into continuing with PDO. I've not been using PHP/MySQL for several years, so I'm rather out of touch with new developments. Your code works fine (: –  Rudy van Sloten Jan 18 at 16:57

Why don't you just do a foreach loop and insert the tags?

foreach($tags as $tag_name)
{
    mysql_query("INSERT INTO tags (tag_name) VALUES ($tag_name)");
}

Note that you should really be using PDO as mysql_query is outdated. This is just an example and you should do a little research on how to sanitize your data before inserting it. A simple Google search will get you going.

share|improve this answer
 
Thank you, I'm currently looking into PDO. I already know how to sanitize my data, but haven't written the code for this yet as I wanted to test basic functionality. –  Rudy van Sloten Jan 18 at 16:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.