I have arrays which can potentially have unlimited amount of elements in them. I am trying to input them into my db but it only inputs the first 3 elements of the arrays. My arrays looks like this
array(4) {
["ID"]=>
array(5) {
[0]=>
string(1) "1"
[1]=>
string(1) "2"
[2]=>
string(1) "3"
[3]=>
string(1) "4"
[4]=>
string(1) "5"
}
["firstname"]=>
array(5) {
[0]=>
string(5) "Steve"
[1]=>
string(3) "Dan"
[2]=>
string(3) "Jim"
[3]=>
string(4) "Adam"
[4]=>
string(5) "James"
}
["surname"]=>
array(5) {
[0]=>
string(5) "Smith"
[1]=>
string(6) "Colins"
[2]=>
string(6) "Knight"
[3]=>
string(5) "Lamar"
[4]=>
string(4) "Rays"
}
["submit"]=>
string(5) "Enter"
}
this is my for loop and sql statement
$a[0] = $_SESSION['ID'];
$a[1] = $_SESSION['firstname'];
$a[2] = $_SESSION['surname'];
for ($i = 0; $i<count($a); ++$i){
$id = $_SESSION['ID'][$i];
$fname = $_SESSION['firstname'][$i];
$sname = $_SESSION['surname'][$i];
$query = "INSERT INTO orders(id, firstname, surname) VALUES('$id', '$fname', '$sname')";
if (mysqli_query($connection, $query)) {
echo "New record created successfully";
} else {
echo "Error: " . $query . "<br>" . mysqli_error($connection);
}
}
this will only post the 0,1,2 elements of each array. How can I change it so it insert all elements to my db?
$a, which only has 3 visible entries. Not sure where else it's supposed to get rows from to insert.$aarray? Other than the erroneouscount($a), you're never using it for anything.