1

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.

1
  • Have you tried anything to achieve it or just posted a question Commented Apr 3, 2015 at 6:30

2 Answers 2

1

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);
}
Sign up to request clarification or add additional context in comments.

Comments

0
$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...

2 Comments

Pls clear me about $items variable. i think ur answer would be really helpful.
just edited my answer.. check it if any query then let me know

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.