I have a complicated piece of code I'm using to make a survery....where I am dynamically creating the names and values of checkboxes. I am using nested while loops to pull the questions from a table, and the choices related to each question from a table. The problem is I am using similar nested while loops to enter the data into the db. The problem is that checkboxes do not submit anything for items which were not checked. My insert statement needs some constraint so it doesnt throw in junk records for where no choice was selected. My while loops are sifting through all the tablees and I need something to determine whether or not a sub-elment of my array has a string lenth of zero...
here is some of my code:
This part of my code is wehre I have the insert statement. If you look towards the end... all the commented code is all the things that I was trying before.
$sql7 = "SELECT * FROM tbl_survey_winter06_questions";
$mysql_result7=mysql_query($sql7);
$num_rows7=mysql_num_rows($mysql_result7);
$count_questions=0;
while ($row7=mysql_fetch_array($mysql_result7))
{
$count_questions++;
$question_id7=$row7["question_id"];
//$question7=$row7["question"];
$sql8 = "SELECT * FROM tbl_survey_winter06_choices WHERE question_id = $question_id7";
$mysql_result8=mysql_query($sql8);
$num_rows8=mysql_num_rows($mysql_result8);
$count_choices=0;
while ($row8=mysql_fetch_array($mysql_result8))
{
$choice_id8=$row8["choice_id"];
$question_id8=$row8["question_id"];
$choice2=$row8["choice"];
$question_submit = $_POST[$question_id7];
$choice_id_submit = $_POST[$choice_id8];
$blank='';
//$condition=$_POST['$question_id7'];
//$zero=0;
//$zerocheck=$_POST['$question_id7'][$count_choices];
//if (isset($_POST['$question_id7'][$count_choices]) && ($zerocheck <> $zero))
//if (isset($_POST['$question_id7'][$count_choices]))
//if ($zerocheck > 0)
//echo $_POST['$question_id7'][1];
//echo "<BR><BR>";
//{
//echo "meow";
//echo $_POST['$question_id7'];
//$query = "INSERT into tbl_survey_winter06_results (vb_id, username, question_id, choice_id) VALUES ".
//"('{$bbuserinfo['userid']}','{$bbuserinfo['username']}',$question_id7,'{$_POST['$question_id7'][$count_choices]}')";
//$pos = strpos($_POST['$question_id7'][$count_choices], $blank);
// Note our use of ===. Simply == would not work as expected
// because the position of 'a' was the 0th (first) character.
//if ($pos === false) {
if (empty($_POST['$question_id7'][$count_choices])) {
//if (in_array("1", $_POST['$question_id7'][$count_choices])){
$count_choices++;
}
else
{
$query = "INSERT into tbl_survey_winter06_results (vb_id, username, question_id, choice_id) VALUES ('{$bbuserinfo['userid']}','{$bbuserinfo['username']}',$question_id7,'". $_POST[$question_id7][$count_choices] ."')";
$mysql_result6=mysql_query($query);
$count_choices++;
echo $query;
$count_choices++;
}
}
//for ( $i = 0; $i <= $count_choices; $i++) {
//
//}
}
If you are wondering why I used these while loops.... its because I was reusing similar code which I used to generate the checboxes in the first place...
If I take out the if statement.. what happens is... all checked items enter perfectly... but blanks are thrown into the choices_id field for all unchecked items...