Reputation: 81
I've this multidimensional array to insert into mysql database :
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 )
)
I've a table named 'student_attendance' and columns are :
'att_id', //it's automatically incremented.
'subject_name' , 'subject_code', 'department_short_name',
'department_name', 'teacher_name', 'date', 'time', 'student_name',
'att_status'
Please help me to insert this array into this mysql table. And This should be done by foreach
looping.
Upvotes: 1
Views: 69
Reputation: 41885
Since its already in batches, just apply a simple foreach loop. I'd suggest PDO with prepared statements:
$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);
}
Upvotes: 1
Reputation: 2447
$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);
try this... where $items would be your variable in which multidimentional array is coming...
Upvotes: 0