-3

I wonder what's the best way to insert data into a database from a multidimensional form. The name of my form inputs is something like below:

name="order[][quantity]"
name="order[][description]"
name="order[][article]"
name="order[][price]"
name="order[][tax]"
name="order[][discount]"

How to insert this data into my database? Below is a image of my form:

enter image description here

As you can see the user can add many sections and all are to be handled by one submit button. Number of orders is a variable.

How do I resolve this?

Shehary
  • 9,926
  • 10
  • 42
  • 71
mrjasmin
  • 1,230
  • 6
  • 21
  • 37

2 Answers2

2

What I'm assuming is..

SomePage.php

<input type='text' name='quantity[]'>
<input type='text' name='description[]'>
<input type='text' name='article[]'>
<input type='text' name='price[]'>
<input type='text' name='tax[]'>
<input type='text' name='discount[]'>

Submit_Some_Page.php

<?
extract($_POST);

$TotalArticle=sizeof($article);

for($i=0;$i<$TotalArticle;$i++)
{
    $Article=$article[$i];
    $Quanity=$quantity[$i];
    $Price=$price[$i];
    $Tax=$tax[$i];
    $Discount=$discount[$i];
    $Description=$description[$i];

    <-- Now, Write Insert Query Here..
    $Query="INSERT INTO TableName SET Col1Name=$Article,Col2Name=$Quanity,Col3Name=$Price,Col4Name=$Tax,Col5Name=$Discount,Col6Name=$Description";
      ..... Write Mysql Query To Execute It
}
?>
Nana Partykar
  • 10,556
  • 10
  • 48
  • 77
  • 1
    ohhh thank you. Exactly what I was looking for but I'm so tired today that I cant think :D – mrjasmin Aug 31 '15 at 17:55
  • I'm Thankful To You @mrjasmin. Atleast, My Code Helped Someone. No Problem.. Sometimes, It Happen. Take Care. – Nana Partykar Aug 31 '15 at 17:57
  • Sorry To Say Mr @dlporter98 It Will Work. I Never Post Any Answer Without Proper Testing. The Person Who Asked Agreed On My Answer. And, What Validation He Has To Do, Let Him Do. As you can see the above image.. Price, Tax and discount are inter-related.. after filling those textbox only TOTAL PRICE is getting calculated. And, Thanku For downvoting my answer without testing properly. – Nana Partykar Aug 31 '15 at 18:51
1

You're going to have to define the array for PHP to follow. In other words, you can't use an empty key for this, you'll need to autonumber them

name="order[0][quantity]"
name="order[0][description]"
name="order[0][article]"
name="order[0][price]"
name="order[0][tax]"
name="order[0][discount]"

And then do order[1] etc

Machavity
  • 30,841
  • 27
  • 92
  • 100