dlcmpls Posted September 13, 2007 Share Posted September 13, 2007 Hi All. I'm a little stuck. I have a web page that lists businesses. Let's say the page display's 5 businesses at a time. Each business has a checkbox that let's the user indicate if they want to "save" that business for future reference. This way the user can generate a list of favorite businesses - like restaurants for example. So out the 5 businesses displayed the user may want to save 1, 3 or 5. I'm stuck on how to do the php/mysql query to insert multiple rows of saved businesses. The bits of data to be save are userid, businessid, savedbusinessid. So if a user decides to save 3 out of the 5 businesses how do I write the php/mysql query to do this? Thanks in advance. dlc Link to comment https://forums.phpfreaks.com/topic/69238-inserting-multiple-rows-of-data/ Share on other sites More sharing options...
GingerRobot Posted September 13, 2007 Share Posted September 13, 2007 You'll be wanting to do a loop. From what you've said, im assuming savedbusinessid is an auto increment field in your table, whilst businessid is the id which identifies each individual business. In which case, you'll probably have an array containing each of the businessids. The query would look something like: <?php foreach($businessid as $v){ mysql_query("INSERT INTO `yourtable` (savidbusinessid,businessid,userid) VALUES('','$v',$userid)") or die(mysql_error()); } ?> Edit: Perhaps the issue is also about creating the array of ids to save. When you output the checkboxes, you should do something like: <?php //am assuming you have some sort of loop to echo all the businesses while($row=mysql_fetch_array($result)){ //other code in the loop echo '<input type="checkbox" name="businessid['.$row['businessid'].']" />' ; } ?> Assuming your form method was POST, you'd then have an array in the $_POST arrray containing all of the business id's selected. I've had to make lots of assumptions here, so dont expect anything to work as is. Link to comment https://forums.phpfreaks.com/topic/69238-inserting-multiple-rows-of-data/#findComment-347954 Share on other sites More sharing options...
dlcmpls Posted September 13, 2007 Author Share Posted September 13, 2007 I think I understand Ginger. All of your assumptions are basically correct. Basically the "for each" will loop through each of the Posted variables thus inserting a record for each variable. Correct? Do I have to have an array established before sending anything to the "foreach" function? Or will that simply loop through ANY posted variable called businessid? Right now, Page1, where all the businesses are listed, has a hidden form variable called "businessid" and that value is populated with a query. That all works fine. Now, from Page1, do I have to have some sort of array on that page to pass all the businessid's or can I simply post Page1 to Page1Processor and the foreach will recognize all the businessid's and loop through them? Thanks. dlc Link to comment https://forums.phpfreaks.com/topic/69238-inserting-multiple-rows-of-data/#findComment-347975 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.