Hi All

I found many article and discussions on storing values of multiple check box here I found one thread Click Here

from which I tried to solve my problem but after doing every thing mentioned in that post finally I reached with an errorError: Column count doesn't match value count at row 1
I under stand the problem DB structure is not matching with the query, but how can solve this problem I am confused...
My DB structure -
Varchar columns are survey_first_name, survey_last_name, survey_school_name, survey_city, survey_sex, survey_email
Int columns are survey_class, survey_stream, survey_computer_subject, survey_frequency, feature1, feature2, feature3, feature4, feature5, feature6, feature7, feature8, feature9, feature10, feature11

Here is my form.php partial code -

<li id="li_10" >
<label class="description" for="element_10">Such type of web site should should have following features <font color="#EC0006">*</font></label>
<span>
<input id="element_10_1" name="feature[]" class="element checkbox" type="checkbox" value="1" />
<label class="choice" for="element_10_1">5 years question paper (Solved)</label>
<input id="element_10_2" name="feature[]" class="element checkbox" type="checkbox" value="2" />
<label class="choice" for="element_10_2">5 years question paper (Unsolved)</label>
<input id="element_10_3" name="feature[]" class="element checkbox" type="checkbox" value="3" />
<label class="choice" for="element_10_3">Sample Questions (approx. 500)</label>
<input id="element_10_4" name="feature[]" class="element checkbox" type="checkbox" value="4" />
<label class="choice" for="element_10_4">Ask their problem via email</label>
<input id="element_10_5" name="feature[]" class="element checkbox" type="checkbox" value="5" />
<label class="choice" for="element_10_5">Online help 24 x 7</label>
<input id="element_10_6" name="feature[]" class="element checkbox" type="checkbox" value="6" />
<label class="choice" for="element_10_6">Sample projects</label>
<input id="element_10_7" name="feature[]" class="element checkbox" type="checkbox" value="7" />
<label class="choice" for="element_10_7">Sample of codes</label>
<input id="element_10_8" name="feature[]" class="element checkbox" type="checkbox" value="8" />
<label class="choice" for="element_10_8">Sample presentations</label>
<input id="element_10_9" name="feature[]" class="element checkbox" type="checkbox" value="9" />
<label class="choice" for="element_10_9">Free Softwares</label>
<input id="element_10_10" name="feature[]" class="element checkbox" type="checkbox" value="10" />
<label class="choice" for="element_10_10">Students can share their work with others</label>
<input id="element_10_11" name="feature[]" class="element checkbox" type="checkbox" value="11" />
<label class="choice" for="element_10_11">Latest updates regarding IT</label>
</span> 

And this is frminsert.php code-

<?php
$host="localhost";
$username="root";
$password="";
$db_name="cbsecsnip";
$tbl_name="survey_tbl";

mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$name=$_POST['element_12'];
$lastname=$_POST['element_13'];
$sex=$_POST['element_11'];
$schoolname=$_POST['element_1'];
$city=$_POST['element_2'];
$email=$_POST['element_5'];
$class=$_POST['element_6'];
$stream=$_POST['element_8'];
$comsubject=$_POST['element_7'];
$required=$_POST['element_9'];
$features=$_POST['feature'];
$frequency=$_POST['element_14'];

$allfeatures = "";
foreach ($features as $f) {
$allfeatures .= $f . ", ";
}
$allfeatures = substr($allfeatures, 0, -2);
echo "<p>The resulting string is: <strong>$allfeatures</strong></p>\r\n";

$sql="INSERT INTO $tbl_name(survey_first_name, survey_last_name, survey_school_name, survey_city, survey_sex, survey_email, survey_class, survey_stream,  survey_computer_subject, survey_frequency, feature1, feature2, feature3, feature4, feature5, feature6, feature7, feature8, feature9, feature10, feature11) VALUES ('$name', '$lastname', '$schoolname', '$city', '$sex', '$email', '$class', '$stream', '$comsubject', '$frequency','$allfeatures')";

$result=mysql_query($sql);

if($result){
echo "Successful";
echo "<BR>";
echo "<a href='insert.php'>Back to main page</a>";
}
else {
die('Error: ' . mysql_error());
}
mysql_close();
?>

Thnak you

Member Avatar for diafol

I'd suggest an alternative method - where you just have the one column for 'feature'

the various checkboxes (1-11) are numbered thus: 1,2,4,8,16,32,64,128,256,512,1024

So any combination will give you an unique number, e.g. checkbox 1,3,7 = 1+4+64 = 69

Here's an example of what I mean - this is a self-conteined snippet, so it should work as a stand-alone script:

<?php
function conv($item){
    return pow(2,$item-1);
}

//IF FORM SENT
if(isset($_POST['submit'])){
    $feature = (isset($_POST['feature'])) ? array_sum(array_map('conv',$_POST['feature'])) : 0;
    echo "POSTED: $feature";
    //write this to the DB
}else{
    echo "Nothing posted so far - so default checkboxes 1,3,6 checked - equivalent to sum of 37)";  
}

//IF GETTING DATA FROM DB
//just for now BUT this is the value you'd get from the single 'feature' column
$dbfeature = (isset($feature)) ? $feature : 37;

$labels = array("5 years question paper (Solved)","5 years question paper (Unsolved)","Sample Questions (approx. 500)","Ask their problem via email","Online help 24 x 7","Sample projects","Sample of codes","Sample presentations","Free Softwares","Students can share their work with others","Latest updates regarding IT");

$checkbox_array = "";
for($x=0;$x<count($labels);$x++){
    $cnt = $x + 1;
    $chkd = ($dbfeature & conv($cnt)) ? ' checked="checked"' : '';
    $checkbox_array .= "\n<input id=\"element_10_$cnt\" name=\"feature[]\" class=\"element checkbox\" type=\"checkbox\" value=\"$cnt\"$chkd /><label class=\"choice\" for=\"element_10_$cnt\">{$labels[$x]}</label>";
}
?>

<form method="post">
<?php echo nl2br($checkbox_array);?>
<input type="submit" name="submit" id="submit" value="submit" />
</form>

Hi Diafol

I got your point and I done as per your guidance. Thank you for your expert guidance

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.