开发者

Save multiple rows with multiple arrays - PHP

开发者 https://www.devze.com 2023-04-11 20:42 出处:网络
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

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 
       开发者_Go百科                     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/


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
                }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号