问题
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:
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?
回答1:
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
}
?>
回答2:
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
来源:https://stackoverflow.com/questions/32316831/inserting-data-into-mysql-table-from-mutlidimensional-input-form