将多个数组保存到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/
答案 0 :(得分:0)
感谢您的回复。我已经找到了解决问题的方法。这是我的代码:
/* 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
}