簡體   English   中英

如何遍歷3維數組以將數據插入數據庫?

[英]How do I loop through a 3 dimensional array to insert data into the database?

這是我的3維數組。

我的$ _POST數組。 這些是價值

$_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 ) )

我如何遍歷它並將每個數組提交到數據庫?

我的列名是鍵。 student_idsubject_idclass_idmark_obtainedmark_obtainable ”。

我正在嘗試這樣做,但我不斷收到重復的條目.` 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;
    }
}

您必須像這樣在foreach循環中回顯sql變量。

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;
}

在回顯時連接變量。

您不需要3維數組。 您可以使用二維數組,並且通過使用foreach可以將它們一一插入。 這是一個例子:

 $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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM