-1

Okay now i have 2 input field on checkbox and one more is input text like this

<input type="text" name="student[]">
<input type="checkbox" name="marks[]">

Now i want to insert the all data in the database.How to insert simultaneously.

this is the example of php want but only one array

foreach($student as $value){

    $sql = "INSERT INTO `academic` ('student', `mark`) VALUES ('$value','mark')";

        if(mysqli_query($conn, $sql)){
            echo "Insert mark for student ".$value." complete<br>";
        } else {
            echo "Error: " . mysqli_error($conn);
        }
    }

how to make it can insert 2 different array simultaneously?

Tahi
  • 85
  • 1
  • 10
  • How about using `foreach` and try out [this solution](https://stackoverflow.com/a/452934/5938111) – Tiến Nguyễn Hoàng May 02 '18 at 05:58
  • Please show us what the expected result (example data in the database) would look like. – M. Eriksson May 02 '18 at 05:59
  • Run two foreach loop. One for `$student` and other for `$marks` which will be inside of `$student`. – Prashant Tapase May 02 '18 at 06:07
  • 1
    @AmrAly - Using PDO doesn't help you against SQL Injections by itself. You need to use Prepared Statements, which you can be done with mysqli as well. – M. Eriksson May 02 '18 at 06:07
  • The OP really needs to show us the expected result before we can give a proper answer. Btw, does the form contain multiple students? If yes, then how do you know which marks goes with which student? Currently, any answer would just be a wild guess with a lot of assumptions. – M. Eriksson May 02 '18 at 06:10
  • @Magnus Eriksson you are correct the prepared statements can be used as well in mysqli – Amr Aly May 02 '18 at 06:46

4 Answers4

1

In this case you can use 2nd foreach for second array like

foreach($student as $value){
 foreach($marks as $singlemarks){

    $sql = "INSERT INTO `academic` ('student', `mark`) VALUES ('$value','$singlemarks')";

        if(mysqli_query($conn, $sql)){
            echo "Insert mark for student ".$value." complete<br>";
        } else {
            echo "Error: " . mysqli_error($conn);
        }
     }
    }

and you can also convert array in json for store multi array in database like json_encode($array) and same as you can decode when you want to use it

Krunal Pandya
  • 204
  • 1
  • 2
  • 12
0

use JSON to store data. just encode array to json and decode when you want to use eg json_encode($array);

Amit B
  • 1
  • 1
0

you need to count value of student and marks and check student values>marks values or student values<marks values .and use it in for loop.after that check if values are exist or not in students and marks array.something like this.

 if (isset($_POST['student'])) {
    $student=$_POST['student'];
    $marks=$_POST['marks'];
    $countStudent=count($student);
    $countMarks=count($marks);
    if ($countMarks>$countStudent) {
        $resultCount=$countMarks;
    }
    else{
        $resultCount=$countStudent;

    }

    for ($i = 0; $i <=$resultCount-1 ; $i++) {
        if (isset($student[$i])) {
            $value= $student[$i];
        }
        else{
            $value= '';
        }
        if (isset($marks[$i])) {
            $mark = $marks[$i];
        }
        else{
            $mark = '';
        }
        $sql = "INSERT INTO `academic` ('student', `mark`) VALUES ('$value','$mark')";
    }
}
Rahul
  • 1,617
  • 1
  • 9
  • 18
-1

Merge the two arrays into a single array. Something like this: $res = array_merge($my_array1, $my_array2);

Then Insert data into database like below:

foreach (array_expression as $key => $value)
{
  //Here Goes Here Your Logic....
}
Debakant Mohanty
  • 805
  • 1
  • 10
  • 19
  • How would that work if the two arrays contains different data which suppose to be inserted in different columns? – M. Eriksson May 02 '18 at 05:59
  • As per the question asked by @Fazidi Ijtihad, one array will be containing the student names and the other will be containing the marks against each student. – Debakant Mohanty May 02 '18 at 06:03
  • Yes. `$_POST['students']` and `$_POST['marks']`. If you merge them, it will result in one array with all the values, without knowing which value is which. – M. Eriksson May 02 '18 at 06:05