多行数据同时提交插入怎么处理或循环

现有一个项目,以班为单位,提交孩子入园情况,如何将多条数据一次写入到表中

Array ( [pubtime] => Array ( [0] => 1459392035 [1] => 1459392035 [2] => 1459392035 [3] => 1459392035 [4] => 1459392035 [5] => 1459392035 [6] => 1459392035 [7] => 1459392035 ) [classname] => Array ( [0] => 托1班 [1] => 托1班 [2] => 托2班 [3] => 托3班 [4] => [5] => 小4班 [6] => 大4班 [7] => 小6班 ) [childname] => Array ( [0] => 程梓煊 [1] => 董明浩 [2] => 白昊冉 [3] => 楚皓扬 [4] => [5] => 张三 [6] => 高晓 [7] => 王小五 ) [iscome] => Array ( [0] => 绿卡 [1] => 绿卡 [2] => 绿卡 [3] => 绿卡 [4] => 绿卡 [5] => 绿卡 [6] => 绿卡 [7] => 绿卡 ) )

如何循环写入



超级S
浏览 1991回答 3
3回答

灬紫羽

$data = Array (     'pubtime' => Array (         '0' => 1459392035,         '1' => 1459392035,         '2' => 1459392035,         '3' => 1459392035,         '4' => 1459392035,         '5' => 1459392035,         '6' => 1459392035,         '7' => 1459392035     ) ,     'classname' => Array (         '0' => '托1班',         '1' => '托1班',         '2' => '托2班',         '3' => '托3班',         '4' => '小4班',         '5' => '小4班',         '6' => '大4班',         '7' => '小6班'     ),     'childname' => Array (         '0' => '程梓煊',         '1' => '董明浩' ,         '2' => '白昊冉' ,         '3' => '楚皓扬' ,         '4' => '张三',         '5' => '张三' ,         '6' => '高晓' ,         '7' => '王小五'     ),     'iscome' => Array (         '0' => '绿卡' ,         '1' => '绿卡' ,         '2' => '绿卡' ,         '3' => '绿卡' ,         '4' => '绿卡' ,         '5' => '绿卡' ,         '6' => '绿卡' ,         '7' => '绿卡'     ) ); $arr = array(); foreach($data as $key=>$value){     foreach($value as $k=>$v){         $arr[$k][$key]= $v;     } } //echo "<pre>"; //print_r($arr);以上部分是对上述数组重新组装,组装格式为 /* Array (     [0] => Array         (             [pubtime] => 1459392035             [classname] => 托1班             [childname] => 程梓煊             [iscome] => 绿卡         )     [1] => Array         (             [pubtime] => 1459392035             [classname] => 托1班             [childname] => 董明浩             [iscome] => 绿卡         )     [2] => Array         (             [pubtime] => 1459392035             [classname] => 托2班             [childname] => 白昊冉             [iscome] => 绿卡         )     [3] => Array         (             [pubtime] => 1459392035             [classname] => 托3班             [childname] => 楚皓扬             [iscome] => 绿卡         )     [4] => Array         (             [pubtime] => 1459392035             [classname] => 小4班             [childname] => 张三             [iscome] => 绿卡         )     [5] => Array         (             [pubtime] => 1459392035             [classname] => 小4班             [childname] => 张三             [iscome] => 绿卡         )     [6] => Array         (             [pubtime] => 1459392035             [classname] => 大4班             [childname] => 高晓             [iscome] => 绿卡         )     [7] => Array         (             [pubtime] => 1459392035             [classname] => 小6班             [childname] => 王小五             [iscome] => 绿卡         ) ) )*/ //下面的代码是连接数据库并写入数据表 $link = mysqli_connect('localhost','root','root','test'); if(!$link){     echo "数据库连接失败";die; } foreach($arr as $key => $value){     $sql = "insert into child(childname,classname,iscome,pubtime) values";     $sql.='("'.$value['childname'].'","'.$value['classname'].'","'.$value['iscome'].'","'.$value['pubtime'].'")';     echo $sql."<br/>";     mysqli_query($link,$sql); } mysqli_close($link); /* 我数据库表结构如下: CREATE TABLE `child` (   `id` int(11) NOT NULL AUTO_INCREMENT,   `childname` varchar(255) CHARACTER SET gbk NOT NULL,   `classname` varchar(255) CHARACTER SET gbk NOT NULL,   `iscome` varchar(255) CHARACTER SET gbk NOT NULL,   `pubtime` int(11) NOT NULL,   PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=25 DEFAULT CHARSET=latin1 | */ //这个只要你数组里有几条数据,就会给你添加几条,不用再去发愁数据条数增加问题了,写的略简单,希望大神们勿喷
打开App,查看更多内容
随时随地看视频慕课网APP