0

I am trying to create a query from a form that passes ID and Details fields, the post array looks like this:

array(2) { 
["ID"]=> array(9) 
    { 
    [0]=> string(3) "10a" 
    [1]=> string(3) "10b" 
    [2]=> string(3) "10c" 
    [3]=> string(3) "10d" 
    [4]=> string(3) "10e" 
    [5]=> string(3) "10f"  
    } 
["Details"]=> array(9) 
    { 
    [0]=> string(19) "This is textfield 1" 
    [1]=> string(17) "This is textfield 2" 
    [2]=> string(0) "" 
    [3]=> string(0) "" 
    [4]=> string(0) "" 
    [5]=> string(0) ""
    } 
}

The ID is hardcoded into the page and always passed, the details can be filled out by the user as needed.

 if ($_POST['OthProb']['Details'] != '') {
        // Catch issue text and assign to variable
        foreach ( $_POST['OthProb']['Details'] as $key => $value) {
            $id = $_POST['OthProb']['ID']->$key;
            $dets = $value;
        // Focus on fields with values and join the array values together in a query string
        if ($dets != ''){
        $string = "INSERT into $table (AccountID, OtherID, ID, Details) VALUES ($_POST[AccountID], $_POST[OtherID], '".$id.", '".$dets."')";
            $sql = $DBH->prepare($string);
            // $sql->execute();
            // var_dump ($id);                
   }   
   }

}

creates the following

 "INSERT into tbl_problems (AccountID, OtherID, ID, Details) VALUES (80, 80, '10f, 'This is Title issue')"

The 'Details' are outputting correctly, but its cycling all the way through the ID's to the last one, is that because I'm nesting a foreach? What would be the best way to structure the loop?

1
  • 1
    Just as a side note, you may want to add an extra single quote in this line (right after the .$id."): $string = "INSERT into $table (AccountID, OtherID, ID, Details) VALUES ($_POST[AccountID], $_POST[OtherID], '".$id.", '".$dets."')"; becomes $string = "INSERT into $table (AccountID, OtherID, ID, Details) VALUES ($_POST[AccountID], $_POST[OtherID], '".$id."', '".$dets."')"; Commented Jun 13, 2013 at 12:41

1 Answer 1

2

Try to replace this line:

$id = $_POST['OthProb']['ID']->$key;

by

$id = $_POST['OthProb']['ID'][$key];
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome, I swear I tried that yesterday, I must have made a mistake elsewhere! Works anyway, thanks very much.

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.