Having troubles with inserting to SQL from an array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
gjb79
Forum Commoner
Posts: 96
Joined: Fri Jul 18, 2003 6:35 am
Location: x <-- (DC)
Contact:

Having troubles with inserting to SQL from an array

Post 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!
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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
User avatar
gjb79
Forum Commoner
Posts: 96
Joined: Fri Jul 18, 2003 6:35 am
Location: x <-- (DC)
Contact:

Post 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!
Post Reply