Inserting data into mySQL table from mutlidimensional input form

空扰寡人 提交于 2019-12-25 19:39:19

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!