我有这个多维数组要插入到 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_attendance”的表,列是:
'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);
}
关于php - 将多维数组插入mysql表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29427270/