php - 将多维数组插入mysql表

标签 php mysql

我有这个多维数组要插入到 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/

相关文章:

php - 如何使顶部导航中的单个链接在 wordpress 中具有不同的背景颜色?

c# - 绑定(bind)datagrid时数据库异常

php - 将数据插入多个表

asp.net - mysql asp.net c# unicode

mysql - 3 Table Join with Clause出现空值

mysql - 推进/Symfony 1.4 : combine securely custom sql subquery with criteria

php - Wordpress:将 ‘srcset’ 和 ‘sizes’ 属性添加到自定义程序中的图像

php - 在php中插入计算代码后出错

php - 从 sqlstate 错误代码中获取 mysql 错误文本

php - 错误: Dynamically fetching css from CDN with fallback to local copy, 如何解决?