0

I need some help to insert comma separates strings into multiple columns into db.

My $_POST['search'] value output. [search] => schools,books,mobile

How do i make an foreach or for to insert those data into db? :S

5
  • 1
    is your question about the foreach loop or about the insertion in the db? Commented Mar 30, 2010 at 22:06
  • 1
    Are you looking for the explode function or do I misunderstand? Commented Mar 30, 2010 at 22:07
  • 1
    What does your table definition look like (z.e. which columns do you have)? Commented Mar 30, 2010 at 22:09
  • i've been looking the explode function. I just don't know how to use it to insert my separates $_POST['search'] values Commented Mar 30, 2010 at 22:11
  • 1
    I see this is tagged mysql. Which db api are you using? Commented Mar 30, 2010 at 22:16

2 Answers 2

1

You can use the explode function.

$queries=explode(',', $_POST['search']

Now $query is an array containing the separated values. Then you do your query, but without further informations about that, I have to stop here.

Sign up to request clarification or add additional context in comments.

Comments

0
                    $post = $_POST['search'];
                $queries = explode( ',', $post );
                foreach( $queries as $query ) {
                    $db->Execute("INSERT INTO tags SET name = '".$query."'");
                }

This seems to worked for me..

I was using @klez's informations

2 Comments

That's multiple rows not multiple columns. Also you shouldn't put raw user input in an SQL query like that. What if the user types php', priority = '3000
As mentioned by Alexandre, you'll want to look into mysql_escape_string/mysql_real_escape_string, or the similarly named functions for mysqli.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.