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?
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
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....
}
use JSON to store data. just encode array to json and decode when you want to use eg json_encode($array);
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')";
}
}