0

Here is a example of what my form looks like.

<div>
<input name="address[1][name]" type="text">
<input name="address[1][street]" type="text">
<input name="address[1][city]" type="text">
<input name="address[1][phone]" type="text">
</div>

<div>
<input name="address[2][name]" type="text">
<input name="address[2][street]" type="text">
<input name="address[2][city]" type="text">
<input name="address[2][phone]" type="text">
</div>

...

I'd like to increment the data obtained from each block in PHP and increment it into my database with MySQL.

What is the best way to achieve this ? I know it generates arrays, but I do not know how to deal with the "double-bracket" method (the form "aaa[x][bbb]" probably has a proper name, which I do not know, I'm sorry).

Thanks.

3
  • The terminology you're look for is "nested associative array". I'm a little confused as to what you mean by increment? Are you talking about incrementing the KEY of the array? If so, for what purpose? Commented Aug 24, 2015 at 13:32
  • Do you mean you would like to loop over the arrays? Commented Aug 24, 2015 at 13:38
  • I'm sorry if I was not clear enough. Thanks anyway for the right terminology ! @Marcovecchio perfectly understood what I was trying to achieve. Commented Aug 24, 2015 at 14:02

2 Answers 2

1

If you want to iterate over all fields using double brackets, the best structure to use are nested foreach's:

$formdata = $_POST['address'];
foreach($formdata as $group)
{
  $SQLFields = array();
  $SQLValues = array();
  foreach($group as $field => $value)
  {
    // Here you have each individual field inside each group, so you can 
    // build the fields of the INSERT statement.
    $SQLFields[] = $field;
    $SQLValues[] = $value;
  }
  // Now assemble everything, and your INSERT is ready.
  $SQL = "insert into table (".
           implode(", ", $SQLFields).
          ") values ('".
           implode("', '", $SQLValues).
          "')";
  // Run the SQL statement the way you want.
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your answer ! It seems perfectly suitable for me. I will try to adapt this.
@Blackounet, no problem! If it works for you, please mark it as the answer to the question.
1

I think you are looking for something like this:

$data = $_POST['address'];
foreach($data as $address) {
    //your address-object (name, street, city, phone)
    var_dump($address);
    //Add your sql-query in here and DO NOT forget to escape your received data
}

2 Comments

Thanks ! I'm not quite familiar with "var_dump", so I won't be able to try this code properly. Hopefully your answer will be useful to someone :)
var_dump does nothing else then printing the information in this variable out to you, so you can inspect it :)

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.