Jump to content

Inserting Multiple rows of data


dlcmpls

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.