1

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?

2
  • You are looping through $a, which only has 3 visible entries. Not sure where else it's supposed to get rows from to insert. Commented Jan 29, 2016 at 23:58
  • What's the purpose of the $a array? Other than the erroneous count($a), you're never using it for anything. Commented Jan 30, 2016 at 1:02

2 Answers 2

2
for ($i = 0; $i<count($a); ++$i)

This cycle will go three times because $a array has 3 items (ID, firstname, surname). Change count($a) to count($a[0]) and it should work. Although this isn't exactly the best solution (what if one of those three arrays has more/less items than the others?...)

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

2 Comments

Thank you this should work because all arrays will have the same amount of elements
How can you be so sure? I suppose the array is input from a form. What if someone changes the form and sends with some inputs missing? You should count on this...
1

you are looping through $a array has 3 items

try this solution

$a[0] = $_SESSION['ID'];
$a[1] = $_SESSION['firstname'];
$a[2] = $_SESSION['surname'];

$b = count($a[0]);

 for ($i = 0; $i<$b; ++$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);
}
}

Comments

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.