tags:

views:

242

answers:

2

Hey.

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

+1  A: 

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.

klez
A: 
                    $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

william
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`
Alexandre Jasmin
As mentioned by Alexandre, you'll want to look into mysql_escape_string/mysql_real_escape_string, or the similarly named functions for mysqli.
NuclearDog