Page 1 of 1

Having troubles with inserting to SQL from an array

Posted: Thu Nov 20, 2003 6:36 am
by gjb79
Hi all. :)

I've been having trouble with inserting to a sql database with the values from an array. What I have tried before has included breaking down the array in to various variables.

However this wont work if I try to upload a lot of variables all at once. I would need to tell sql to check hundreds of variables every time, even if one or two contain something. very in efficiant so I scraped it before I even got started. Next I tried to us a do while loop on the entire insert script, so that it would run the insert script for each item in the array. This doesn't seem to work either.

I have a series of check boxes, all with the same name and different values.
This should give me an array containing all the values that where checked from the form right?
:?: If so, then how can I get php and sql to insert a new row into a database table for each value listed in that array?

Thank you for your help!

Posted: Thu Nov 20, 2003 6:52 am
by JayBird
here is an out line for the structure of your code

Code: Select all

// Make DB connect first then...

foreach ($your_array as $temp_value) {
   $sql = "INSERT INTO your_db(array_values) VALUES(".$temp_value.")";
   mysql_query($sql, $link) or die (mysql_error());
}
Thats a rough guide to get you started, not complete code.

Mark

Posted: Thu Nov 20, 2003 10:44 pm
by gjb79
I tried that out, doesn't seem to work. Its very simular to what I had already been trying.

Thanks for the ideas though!

:idea: I just stumbled across an article at PHPBuilder.com that seems to fit the discription of what I'm looking for.

I'm fairly good at the basics in php, beginning to dive deeper, so the code he has on his page looks very complex to me. If you look it over and have any ideas on how it could be simplified I'd appreciate it!

:arrow: http://www.phpbuilder.com/columns/tim19 ... int_mode=1

Thanks!