简体   繁体   English

foreach多维数组并插入数据库

[英]foreach multidimensional Array and insert database

got a problem. 有个问题。 I got form like this 我有这样的形式

<input name="product[sub_name][]">
<input name="product[price][]">
<input name="product[random][]">
<input name="product[just_field][]">

I can add many blocks of this form pressing "add more". 我可以按“添加更多”来添加这种形式的许多块。 Recieving posta data i do the stuff. 接收邮政数据我做的事。

$field = $_POST['product'];

foreach ($field as $key => $values) {

    foreach($values as $value) {

        $key.' - '.$value;


    }

}

I need code to insert multiple rows in database depending on posted rows. 我需要代码以根据发布的行在数据库中插入多行。 Problem is that, i dont know how to get only, for example, "price". 问题是,我不知道如何获得,例如“价格”。 Goal is insert all data in database. 目标是将所有数据插入数据库。 Hope you guys understand my logic. 希望你们能理解我的逻辑。

Here is print_r output. 这是print_r的输出。 I can got more possibilities than two 我有两个以上的可能性

Array ( 
    [sub_name] => Array ( [0] => New car [1] => New bike )
    [standart_price] => Array ( [0] => 100 [1] => 300 )
    [cupon_price] => Array ( [0] => 50 [1] => 200 )
    [max_purchases] => Array ( [0] => 1000 [1] => 100 )
    )

Try to use 尝试使用

print_r($_POST["product"]);

to output the whole array. 输出整个数组。

Are you sure the values transmitted by the formular are passed into $_POST ? 您确定由公式编制程序传输的值传递到$ _POST吗?

Could you please post the output. 你能发布输出吗?

Check this out.. 看一下这个..

<?

//Connect to DB.
$link = mysqli_connect("HOST","USERNAME","PASSWORD","DATABASE") or die("Error " . mysqli_error($link));

//Consider your table structure in MYSQL. Don't depend on this structure.
//CREATE TABLE TBL1(SLNO PRIMARY KEY AUTO_INCREMENT, DETAIL_VALUE VARCHAR(200));
foreach( $_POST['product'] as $key => $value )
{
    //Get specific Tag.
    if( $key == 'price')
    {
        //Creating Query to insert. 
        $query = "insert into TBL1('DETAIL_VALUE') VALUES('".addslashes($value)."')";
        $mysqli_query($link, $query) or die;
    }
}


?>

For more details on querys or php: Refer PHP.net. 有关查询或php的更多详细信息:请参考PHP.net。

$mysqli = new mysqli("localhost", "root", "password", "db name");

$field = $_POST['product'];

foreach ($field['price'] as $idx => $price)
{
    $sub_name = $field['sub_name'][$idx];
    $random = $field['random'][$idx];
    $just_field = $field['just_field'][$idx];

    $sql = "INSERT INTO table (sub_name, random, just_field, price) VALUES ('{$sub_name}','{$random}','{$just_field}','{$price}')";

    $mysqli->query($sql);


}

You can get a more ordered result if you re-organize the array to contain the index first: 如果重新组织数组以首先包含索引,则可以获得更有序的结果:

<input name="product[$index][sub_name]">
<input name="product[$index][price]">
<input name="product[$index][random]">
<input name="product[$index][just_field]">    

Each time you add a new product change the index with javascript, in that way, when you recieve the data in php you can do something like: 每次添加新产品时,都可以使用javascript更改索引,以这种方式,当您在php中接收数据时,可以执行以下操作:

$products = $_POST['product'];

foreach ($products as $product)
{
    $sub_name = $product['sub_name'];
    $random = $product['random'];
    $just_field = $product['just_field'];

    $sql = "Your SQL query"

    $mysqli->query($sql);
}

Maybe need a little more work changing the html indexes with javascript but your code become more clear. 可能需要做更多的工作才能使用javascript更改html索引,但是您的代码变得更加清晰。

ps That's the general idea, i don't test it. ps这是一般的想法,我不测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM