使用多个数组保存多行 - PHP

发布于 2024-12-08 22:44:23 字数 877 浏览 0 评论 0原文

我在将具有多个数组的多行保存到 MYSQL 中时遇到一个大问题。例如,第 1 行包含“名称”和“份额百分比”。然后他们添加另外 2 行,其中包含与上述相同的属性。那么如何将这些数据保存到DB中呢?下面是我不成功的代码:

foreach($_POST['name_members'] as $dir){ // array 1
    $directorID = run_num('director_id','proc_director'); // generate running number for each row
    foreach($_POST['share_percentage'] as $share) { //array 2
        $insDirector = "INSERT INTO 
                            proc_director(director_id, vendor_cd, director_name, director_percentage)
                        VALUES
                            ('$directorID','$vendorID','".trim(addslashes($dir))."','$share')"; 
        $db->query($insDirector); // save the array value into DB
    }                            
}

我做了一个演示界面,这样你就可以得到我想要的图片。这里是超链接:http://softboxkid.com/blog/code/add_row/

I have a big problem on saving multiple rows with multiple arrays into MYSQL. For example row 1 contains "name" and "share percentage". Then they add another 2 rows which contains same attributes as mentioned. So how do I save these data into DB. Below was my unsuccessful code:

foreach($_POST['name_members'] as $dir){ // array 1
    $directorID = run_num('director_id','proc_director'); // generate running number for each row
    foreach($_POST['share_percentage'] as $share) { //array 2
        $insDirector = "INSERT INTO 
                            proc_director(director_id, vendor_cd, director_name, director_percentage)
                        VALUES
                            ('$directorID','$vendorID','".trim(addslashes($dir))."','$share')"; 
        $db->query($insDirector); // save the array value into DB
    }                            
}

I made demo interface, so that you can get the picture what I want. Here the hyperlink: http://softboxkid.com/blog/code/add_row/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幽蝶幻影 2024-12-15 22:44:23

感谢您的回复。我已经找到了我的问题的解决方案。这是我的代码:

/* save partnerhip information */
                $count_director = count(array_trim($_POST['name_members']));

                for($i=0; $i<$count_director; $i++) {
                    $directorID[] = run_num('director_id','proc_director'); // generate running number for each row

                    $insDirector = "INSERT INTO proc_director(director_id, vendor_cd, director_name, director_percentage)
                                         VALUES('".$directorID[$i]."','".$vendorID."','".$_POST['name_members'][$i]."','".intval($_POST['share_percentage'][$i])."')";
                    $db->query($insDirector); // save the array value into DB
                }

Thank for your respond. I already found the solution for my problem. Here is my code:

/* save partnerhip information */
                $count_director = count(array_trim($_POST['name_members']));

                for($i=0; $i<$count_director; $i++) {
                    $directorID[] = run_num('director_id','proc_director'); // generate running number for each row

                    $insDirector = "INSERT INTO proc_director(director_id, vendor_cd, director_name, director_percentage)
                                         VALUES('".$directorID[$i]."','".$vendorID."','".$_POST['name_members'][$i]."','".intval($_POST['share_percentage'][$i])."')";
                    $db->query($insDirector); // save the array value into DB
                }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文