0

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;
    }
}
3
  • 2
    looks like a 2D array to me. Will each entry in the main array be a single row in the database? If so then you just need a single foreach loop, and each one will execute an INSERT statement. Should we assume that the array shown in the first snippet is actually the $_POST array? Commented May 6, 2018 at 19:37
  • Yes. The first snippet is the $_POST array. Commented May 6, 2018 at 19:43
  • Ok so as I suspected I think you only need one loop Commented May 6, 2018 at 20:02

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This is correct and will work, but note that this is extremely vulnerable to SQL injection attacks. Really, parameterised queries and prepared statements should be used instead. See bobby-tables.com for an explanation of the risks, and also some examples of writing queries safely using PHP with mysqli or PDO
0

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

1 Comment

I'm not convinced that executing a query inside a loop is such a great idea. And see about prepared statements- there's some really clever examples of their use in conjunction with multi-row inserts.

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.