在mysql表中插入多维数组


Insert Multidimentional array into mysql table

我有一个多维数组要插入mysql数据库:

Array ( 
[0] => Array 
      ( [0] => Mechanics of Solids [1] => 257 [2] => Civil Engineering
        [3] => CEN [4] => Golam Kibria uddin [5] => 02-APR-2015 [6] => 
        1:30am [7] => Md. Tushar Ahmed [8] => present ) 
[1] => Array 
      ( [0] => Mechanics of Solids [1] => 257 [2] => Civil Engineering 
        [3] => CEN [4] => Golam Kibria uddin [5] => 02-APR-2015 [6] => 
        1:30am [7] => Mrs. Monira Akter [8] => absent ) 
[2] => Array 
      ( [0] => Mechanics of Solids [1] => 257 [2] => Civil Engineering 
        [3] => CEN [4] => Golam Kibria uddin [5] => 02-APR-2015 [6] => 
        1:30am [7] => JOYNAB AKTER [8] => leave ) 
[3] => Array 
      ( [0] => Mechanics of Solids [1] => 257 [2] => Civil Engineering 
        [3] => CEN [4] => Golam Kibria uddin [5] => 02-APR-2015 [6] => 
        1:30am [7] => BEAUTY AKTER [8] => leave ) 
[4] => Array 
      ( [0] => Mechanics of Solids [1] => 257 [2] => Civil Engineering 
        [3] => CEN [4] => Golam Kibria uddin [5] => 02-APR-2015 [6] => 
        1:30am [7] => PURABI BARUA [8] => absent ) 
[5] => Array 
       ( [0] => Mechanics of Solids [1] => 257 [2] => Civil Engineering 
        [3] => CEN [4] => Golam Kibria uddin [5] => 02-APR-2015 [6] =>
        1:30am [7] => SETU BISWAS [8] => present ) 
  ) 

我有一个名为"student_assistance"的表,列为:

  'att_id', //it's automatically incremented.
  'subject_name' , 'subject_code', 'department_short_name',   
  'department_name', 'teacher_name', 'date', 'time', 'student_name', 
  'att_status'

请帮我把这个数组插入这个mysql表中。这应该通过foreach循环来完成。

由于它已经是批量的,只需应用一个简单的foreach循环。我建议PDO准备好声明:

$db = new PDO('mysql:host=localhost;dbname=database_name', 'username', 'password');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
$insert = $db->prepare(
    'INSERT INTO table_name (subject_name , subject_code, department_short_name,   
  department_name, teacher_name, date, time, student_name, att_status) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)');
);
foreach($your_array as $values) {
    $insert->execute($values);
}
$sql="insert into student_attendence(att_id,subject_name,subject_code,department_short_name,department_name,teacher_name,date,time,student_name,att_status) values";
$items_sql = array();
    if (count($items)) {
        foreach ($items as $item) {
            $items_sql[] = "('', '{$item[0]}','{$item[1]}','{$item[2]}'....... and so on)";
        }
    }
    $sql .= implode(", ", $items_sql);

试试这个。。。$items将是多维数组的变量。。。