簡體   English   中英

如何在多行PHP中插入數組內容

[英]How to insert array contenent in multiple rows PHP

在我的動態表中,用戶使用“保存”按鈕在其中添加或刪除行,從而向類發送數組。 這些載體對應於從細胞中去除的內容。

有關其結構的一個小例子:

    input1.name = "product[]";

    input2.name = "seller[]";

    input3.name = "description[]"

    ... etc.

在這里,我們有分發多個產品系列的代碼。 我要確保在要創建的數據庫中的表中(例如:如果您放置兩個產品,則表示數據庫中的表中有2行)。

產品1:數據庫的第一表行產品2:數據庫的第二表行

你能做到嗎? 在下面附加的代碼中,我很難在foreach循環中執行此操作,我應該在每個周期內使用查詢進行插入,但是我從來沒有這樣工作過。

這是該類中的代碼:

public function execute_Insert()
{
    if (isset($_POST['sending']))
    {
        if ($_POST['sending'] == "save")
        {
            foreach($_POST['product'] as $key => $value)
            {
                $product = $_POST['product'];
                print "product key=$key value=$value<br/>";

                foreach($_POST['seller'] as $key => $value)
                {
                    $seller = $_POST['seller'];
                    print "seller key=$key value=$value<br/>";

                    foreach($_POST['description'] as $key => $value) 
                    {
                        $description = $_POST['description'];
                        print "description key=$key value=$value<br/>";

                        foreach($_POST['quantity'] as $key => $value) 
                        {
                            $quantity = $_POST['quantity'];
                            print "quantity key=$key value=$value<br/>";

                            foreach($_POST['priece'] as $key => $value)
                            {
                            $priece = $_POST['priece'];
                            print "priece key=$key value=$value<br/>";

                            } 
                        } 
                    }
                }
            }

我不知道我是否正確理解你,但是如果我是對的,這就是我應該怎么做:

$prod = htmlentities($_POST['product']); // this is an array
$seller = htmlentities($_POST['seller']);
// then do foreach
foreach ($prod as $p) {
    // do the insert in sql
}
foreach ($seller as $s) {
    // do the insert in sql
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM