I need help getting this data into the database and have each records for each row in a different table, My approach was to add second id for each students called unique and then insert it into the database using seal as the primary key instead using the id. My problem is how to get each users records and associate them with their ids or the unique id i created and insert them in a different table called students_meta_table,
Here is the data in array:
Array
(
[0] => Array
(
[first_name] => James
[last_name] => Mike
[age] => 20
[gender] => male
[student_id] => 1
[records] => Array
(
[0] => Array
(
[term] => First term
[scores] => Array
(
[0] => Array
(
[maths] => 207
[english] => 104
[science] => 80
)
)
)
)
)
[1] => Array
(
[first_name] => Luke
[last_name] => Garry
[age] => 21
[gender] => male
[student_id] => 2
[records] => Array
(
[0] => Array
(
[term] => First term
[scores] => Array
(
[0] => Array
(
[maths] => 200
[english] => 109
[science] => 96
)
)
)
)
)
[2] => Array
(
[first_name] => Zara
[last_name] => Eben
[age] => 22
[gender] => female
[student_id] => 3
[records] => Array
(
[0] => Array
(
[term] => First term
[scores] => Array
(
[0] => Array
(
[maths] => 205
[english] => 108
[science] => 76
)
)
)
)
)
[3] => Array
(
[first_name] => Festus
[last_name] => Amrem
[age] => 19
[gender] => male
[student_id] => 4
[records] => Array
(
[0] => Array
(
[term] => First term
[scores] => Array
(
[0] => Array
(
[maths] => 202
[english] => 101
[science] => 94
)
)
)
)
)
[4] => Array
(
[first_name] => Sean
[last_name] => Thomas
[age] => 23
[gender] => male
[student_id] => 5
[records] => Array
(
[0] => Array
(
[term] => First term
[scores] => Array
(
[0] => Array
(
[maths] => 198
[english] => 121
[science] => 105
)
)
)
)
)
)
And this is the code i wrote to add only the other details to the database but the records i dont know how to go about them
foreach($data as $dat){
// Create unique code and must be inside a loop as to create unique numbers for each row
$uniq1 = date(strtotime('NOW'));
$uniq2 = rand(100000, 999999);
$unique = $uniq1 . $uniq2;
$firstname = $dat['first_name'];
$lastname = $dat['last_name'];
$age = $dat['age'];
$gender = $dat['gender'];
$studentId = $dat['student_id'];
$records = $dat['records'];
// REBUILD THE DATA FOR EASY INSERTION
$values[] = "('$unique', '$firstname', '$lastname', '$age', '$gender', '$studentId')";
}
$sql = "INSERT INTO student_table (unique, first_name, last_name, age, gender, student_id) VALUES ".implode(", ", $values);
if($conn->query($sql) === TRUE){
echo 'Data was loaded into the database';
}else{
echo $sql .'<br>'. $conn->error;
}
$conn->close();