0

I have a loop to go through an array and each checkbox that is selected I want to put the value (also brought in dynamically) into my QuestionSelected table. I get this error "Warning: Invalid argument supplied for foreach()" and I cannot get the results into my table. Here is the code I am trying:

//Declare the QuestionID as a array
$QuestionID = array();


while($row = mysqli_fetch_array($run,MYSQLI_ASSOC)){
echo '<div id="QuestionSelection"><input id="chkQuestion" type="checkbox" value=" '.$row['QuestionID'].'" name="question_'.$row['QuestionID'].'">' .$row['Question']. '</p></div><br/><br/>';

//Assign the QuestionID from the table to the var
$QuestionID[] = $row['QuestionID'];

}


if($_POST['submitted']) { 


$ids_list = '';

foreach($_POST["QuestionID"] as $id)
{
$ids_list .= (strlen($ids_list) > 0 ? ',' : '').mysql_real_escape_string($id);
}

$sql2 = "INSERT INTO tbl_QuestionSelected (`QuestionID`) VALUES (".$ids_list.")";

}//End of IF 'submitted
9
  • 8
    $_POST["QuestionID"] isn't an array. Commented Jul 12, 2012 at 16:03
  • 2
    Please don't use mysql_* functions in new code. They were removed from PHP 7.0.0 in 2015. Instead, use prepared statements via PDO or MySQLi. See Why shouldn't I use mysql_* functions in PHP? for more information. Commented Jul 12, 2012 at 16:05
  • input name should be QuestionID[] Commented Jul 12, 2012 at 16:06
  • If I do a print_r(QuestionID) it returns 3 results so it looks as though its populating correctly? Wouldn't this be the right way to see? Commented Jul 12, 2012 at 16:06
  • do print_r($_POST['QuestionID']); as this is what you are foreach-ing Commented Jul 12, 2012 at 16:08

2 Answers 2

1

The problem is that in your form, you are not using an array, but building the name values like:

name="question_'.$row['QuestionID'].'"

So $_POST["QuestionID"] does not exist and is not an array.

You can change your name building to:

name="QuestionID[]"

So that it is an array and you can even use the actual id as a key:

name="QuestionID[' . $row['QuestionID'] . ']"
Sign up to request clarification or add additional context in comments.

2 Comments

I was under the assumption that my attempt is building name values and value values both (as ridiculous as that sounds) I wanted each checkbox to have a new name and then the value be my questionID from my table
I just did print_r($QuestionID); and got the result: Array ( [0] => 1 [1] => 2 [2] => 3 ) before I try this foreach loop. So my array values are in $QuestionID based on my understanding. So my issue is with the foreach and something I have done there. I just can't figure out what. Sorry if I'm missing something in your explaination
0

This resolves the issue and works:

$ids_list = '';

foreach($_POST["QuestionID"] as $key=>$value) {
{
$ids_list .= (strlen($ids_list) > 0 ? ',' : '').mysql_real_escape_string($value);
}

$sql2 = "INSERT INTO tbl_QuestionSelected (`QuestionID`) VALUES (".$ids_list.")";

//Run the query 
$run2 = @mysqli_query ($conn,$sql2);


}

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.