dsw7547
dsw7547
2018-05-06 19:29
浏览 221
已采纳

如何循环三维数组以将数据插入数据库?

This is my 3 dimensional array.

My $_POST array. These are the values

$_POST = Array ( 
[9] => Array ( [student_id] => 9 [subject_id] => 6 [class_id] => 2 [mark_obtained] => 40 [mark_obtainable] => 100 ) 
[10] => Array ( [student_id] => 10 [subject_id] => 6 [class_id] => 2 [mark_obtained] => 52 [mark_obtainable] => 100 ) 
[11] => Array ( [student_id] => 11 [subject_id] => 6 [class_id] => 2 [mark_obtained] => 30 [mark_obtainable] => 100 ) 
[12] => Array ( [student_id] => 12 [subject_id] => 6 [class_id] => 2 [mark_obtained] => 68 [mark_obtainable] => 100 ) )

How do i loop through it and submit each of the array into the database?

My column names are the keys. "student_id, subject_id, class_id, mark_obtained, mark_obtainable".

I am trying this but i keep getting duplicate entries.`foreach ($_POST as $i => $values1) {

foreach ($values1 as $key => $value) {
    $columns = implode(", ",array_keys($values1));
    $values  = implode(", ", $values1);
    $columns = $columns
    $sql = "INSERT INTO `daily_report`($columns) VALUES ($values)";

    if ($conn->multi_query($sql) === TRUE) {
        echo "New records created successfully <br>";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • draxu26480
    draxu26480 2018-05-07 16:43
    已采纳

    You have to echo the sql variable inside the foreach loop like this.

    foreach ($_POST as $data => $values) {
    
    echo $sql = "INSERT INTO `daily_report` (student_id, subject_id, class_id,mark_obtained,mark_obtainable,daily_report.date)
    VALUES (".$values['student_id'].", ".$values['subject_id'].", ".$values['class_id'].", ".$values['mark_obtained'].",".$values['mark_obtainable'].", now())";
    
    // echo $sqlupdate1 = "UPDATE table SET commodity_quantity=$qty WHERE user='".$rows['user']."' ";
    if ($conn->query($sql) === TRUE) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
    

    Concatenate the variables while echoing.

    点赞 评论
  • duanhong8839
    duanhong8839 2018-05-06 19:56

    You don't need a 3 dimensional array. You can use a 2 dimensional array and by using foreach you can insert them one by one. Here is an example:

     $data_array = [
        [
            'student_id' => 9,
            'subject_id' => 6, 
            'class_id' => 2,
            'mark_obtained' => 40,
            'mark_obtainable' => 100    
        ],
            [
            'student_id' => 10,
            'subject_id' => 6, 
            'class_id' => 2,
            'mark_obtained' => 52,
            'mark_obtainable' => 100    
        ],
            [
            'student_id' => 11,
            'subject_id' => 6, 
            'class_id' => 2,
            'mark_obtained' => 30,
            'mark_obtainable' => 100    
        ],
            [
            'student_id' => 12,
            'subject_id' => 6, 
            'class_id' => 2,
            'mark_obtained' => 30,
            'mark_obtainable' => 100    
        ],
    ];
    
    foreach($data_array as $data) {
    $sql = "INSERT INTO `daily_report` (student_id, subject_id, class_id,mark_obtained,mark_obtainable)
    VALUES ($data['student_id'], $data['subject_id'], $data['class_id'], $data['mark_obtained'],$data['mark_obtainable'])";
    if ($conn->query($sql) === TRUE) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
    
    点赞 评论

相关推荐