I have been really confused as to why the folowing program is not working properly.
I pull the information from a page and I do not know how many selections the person may make.(20 possibilities exist, but a max of 7 can be chosen) I originally was going to INSERT all the data into the DB but that didn't work correctly, like so:
FOR ($x=0; $x<=19;$x++){
IF (isset($q[$x])){
$questions_selected++;
}
}
echo $questions_selected;
IF ($questions_selected>7){error_message("You may only select 7 or fewer items");};
$result=mysql_query("INSERT INTO srv VALUES($selected_info[0],$selected_info[1],$selected_info[2],$selected_info[3],$selected_info[4],$selected_info[5],$selected_info[6])",$link_id);};
It would only submit the data when the maximum of 7 choices were selected.
In my table that I am inserting data, all the fields allow nulls to exist.
The first thing that I tried was this:
FOR ($x=0; $x<=19;$x++){
IF (isset($q[$x])){
$selected_info[]= $q[$x];
echo $selected_info[$x]."was a question you selected<br>";
$questions_selected++;
}
}
echo $questions_selected;
IF ($questions_selected>7){error_message("You may only select 7 or fewer items");};
IF ($questions_selected==7){
$result=mysql_query("INSERT INTO srv(q1,q2,q3,q4,q5,q6,q7) VALUES($selected_info[0],$selected_info[1],$selected_info[2],$selected_info[3],$selected_info[4],$selected_info[5],$selected_info[6])",$link_id);};
IF ($questions_selected==6){
$result=mysql_query("INSERT INTO srv(q1,q2,q3,q4,q5,q6) VALUES($selected_info[0],$selected_info[1],$selected_info[2],$selected_info[3],$selected_info[4],$selected_info[5]",$link_id);};
IF ($questions_selected==5){
$result=mysql_query("INSERT INTO srv(q1,q2,q3,q4,q5) VALUES($selected_info[0],$selected_info[1],$selected_info[2],$selected_info[3],$selected_info[4])",$link_id);};
IF ($questions_selected==4){
$result=mysql_query("INSERT INTO srv(q1,q2,q3,q4) VALUES($selected_info[0],$selected_info[1],$selected_info[2],$selected_info[3])",$link_id);};
IF ($questions_selected==3){
$result=mysql_query("INSERT INTO srv(q1,q2,q3) VALUES($selected_info[0],$selected_info[1],$selected_info[2])",$link_id);};
IF ($questions_selected==2){
$result=mysql_query("INSERT INTO srv(q1,q2) VALUES($selected_info[0],$selected_info[1])",$link_id);};
IF ($questions_selected==1){
$result=mysql_query("INSERT INTO srv(q1) VALUES($selected_info[0])",$link_id);};
IF (empty($questions_selected)){error_message("You must select at least 1 item");};
This is giving me some really strange results. It works BUT, rather than just submitting the data one time, is submits for the correct value and each value beneath it. Like so:
For values submitted.
Table Results
1 2 3 4
1 2 3 null
1 2 null null
1 null null null
I'm baffled. From what I can see, there is only one condition that is satisfied if the 4 choices were chosen.
Thanks,
Bryan