Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have the following array:

$array[0] = "internet";
$array[1] = "renweb";

and so on.

I want to insert that array into a table. I do not know how many positions the array will have as this is a post coming from a form that a user fills. How can I insert them into a table?

I was thinking on a foreach

something like:

foreach($array as $tags){
    $query = sprintf("insert into solution_tags values('%s')", $tags);
    $DBconnect->query($query);
}

Is it the correct approach? Or is there a more easy, efficient, painless way to do it?

Any help will be much appreciated!

share|improve this question
 
this is a good way to do it. –  Aris Oct 27 '13 at 19:55
add comment

4 Answers

You could save the array to a textfile and then do a bulk insert:

$file=.... /* your temp filename */
file_put_contents($file,implode("\n",$array));
$DBconnect->query("LOAD DATA INFILE '$file' INTO TABLE solution_tags");

...provided, of course, that the web server and the DB server share the same filesystem!

share|improve this answer
add comment

To answer the question you asked:

Do look for the painless approach only if you feel a pain. Do you? Express it then, to make others understand what certainly makes you uneasy, to let them offer certain solution for this very issue. If not - just leave it as is. Exactly the same goes for the efficiency. And easiness too. Altering your code without a reason, just out of a whim, out of nowhere, may cause much more troubles than you imagine with your current code.

To answer the question you didn't ask:

Your approach, as well as in all the answers, suffers from one essential flaw: you don't format your query properly. Which may lead to unpredictable consequences. To format your query properly you have to use prepared statements

$stm = $DBconnect->prepare("insert into solution_tags values(?)");
$stm->bind_param('s', $tag);

foreach($array as $tag){
    $stm->execute();
}

again: this is not a matter of "efficiency" or "ease" (as a matter of fact, on a more or less complex query this approach will be a pain compared to your current one), but matter of essential approach you ought to follow with every your query.

On a side note, you may wish to add some field(s) to this table to link these tags to some other entities.

share|improve this answer
 
I might be wrong but bind_param should be inside of loop, right? –  speccode Oct 28 '13 at 8:56
 
if you have any doubts - just run the code and see. –  Your Common Sense Oct 28 '13 at 9:08
 
Yup, I was wrong. Sorry. –  speccode Oct 28 '13 at 9:29
add comment

The best way is using foreach like this :

foreach( $myarray as $tags) {
mysql_query('insert into solution_tags values('$tags')');
}
share|improve this answer
 
You just un-sanitized user input. –  geomagas Oct 27 '13 at 19:53
 
This is pretty much the same thing I did on my query. –  Yisera Oct 27 '13 at 20:11
 
@Yisera: You can safely remove the "pretty much" part. Also, it uses deprecated mysql_ stuff. –  geomagas Oct 27 '13 at 20:13
add comment

You can do many insert's with one query:

$aValues = array();
foreach($array as $tags) {
    $aValues[] = sprintf('("%s")');
}

$query = 'INSERT INTO solution_tags VALUES '.implode(',', $aValues);
$DBconnect->query($query);

I used implode because it will handle commas for you.

share|improve this answer
 
Ok, and why this approach is wrong? –  speccode Oct 28 '13 at 6:39
 
Because all the participants of this answer have to read this question: stackoverflow.com/questions/60174/… –  Your Common Sense Oct 28 '13 at 6:40
 
Why of course that PDO is much better solution for DB but still... Let's assume that we have PDO here. You prefere to make 12 $sth->execute() or one? –  speccode Oct 28 '13 at 6:43
 
Your latest comment is just irrelevant, sorry. And your answer is still has unacceptable quality. –  Your Common Sense Oct 28 '13 at 6:46
add comment

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.