Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-04-2006, 10:10 PM   PM User | #1
mattyboi
New Coder

 
Join Date: Mar 2006
Location: Vienna, VA
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
mattyboi is an unknown quantity at this point
Adding multiple records to a table in one form.

Okay, was not quite sure what topic to post this on.

I have one form in a php page. In that form there are several rows, each with several input boxes. These rows represent individual products and their price. You may add as many products as you want by clicking a add product button and it will add another row using javascript. When you are finished adding your products you then click submit. For each product I would like it to insert an individual record in the products table.

For example if you add 10 products, when you click submit it will add 10 records in the product table.

Can you give me some direction in the matter.

Thanks
mattyboi is offline   Reply With Quote
Old 05-05-2006, 02:32 AM   PM User | #2
guelphdad
Super Moderator


 
guelphdad's Avatar
 
Join Date: Mar 2006
Location: St. Catharines, Ontario Canada
Posts: 2,629
Thanks: 4
Thanked 147 Times in 138 Posts
guelphdad will become famous soon enoughguelphdad will become famous soon enough
so in your form you would have some kind of place holder so you know how to identify each row right? say rowid or whatever?

you would then do one of two things, either create an array that holds all of your values and then in your sql you would step through the array to insert each row OR you would create a loop and do a single sql insert for each row.
guelphdad is offline   Reply With Quote
Old 05-05-2006, 02:54 PM   PM User | #3
degsy
Senior Coder

 
Join Date: Nov 2002
Location: North-East, UK
Posts: 1,265
Thanks: 0
Thanked 0 Times in 0 Posts
degsy is on a distinguished road
You need to give the textbox the Record ID, or something that will uniquely identify it.

Because HTML requires a letter to start a name or ID then you will have to name your boxes accordingly and then split the name after submit.

PHP Code:
<form name="form1" method="post" action="">
  <p>
    <input name="id_1" type="text" id="id_1" value="30">
</p>
  <p>
    <input name="id_23" type="text" id="id_23" value="50">
    <input name="text" type="text" id="id_23" value="tezxt">
  </p>
  <p>
    <input name="id_45" type="text" id="id_45" value="4">
  </p>
  <p>
    <input type="submit" name="Submit" value="Submit">
</p>
</form>
<?php
if(isset($_POST['Submit'])){
    foreach(
$_POST as $key => $val){
        if(
strpos($key'id_') !== false){;
            
$id split('id_'$key);
            
//echo $id[1] . ': ' . $val . '<br>';
            
$sql "INSERT INTO table (id, amount) VALUES (" $id[1] . "'," $val ")";
            echo 
$sql '<br>';
        }
    }
}
?>
degsy is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:51 AM.


Advertisement
Log in to turn off these ads.