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 -

Dani AI

Generated

The immediate cause of "Column count doesn't match value count at row 1" is that your INSERT lists 21 columns but supplies only 11 values. Those 11 include a single string $allfeatures, but your table has 11 separate columns (feature1..feature11). MySQL expects one value per listed column, in order. Quick fix if you keep the 11 feature columns: turn the posted checkbox values into 11 flags (0/1) and pass each flag as its own value.

// turn checked IDs (1..11) into 11 flags
$checked = array_map('intval', $_POST['feature'] ?? []);
$flags = array_fill(1, 11, 0);
foreach ($checked as $fid) {
    if ($fid >= 1 && $fid <= 11) $flags[$fid] = 1;
}

// then INSERT ... feature1, feature2, ... feature11
// VALUES (..., $flags[1], $flags[2], ..., $flags[11])

That said, I’d strongly consider a cleaner schema. Instead of 11 columns, create a junction table so you store one row per selected feature. It scales, is easy to query, and avoids schema edits if you add feature12 later. Keep your survey_tbl for the person data, and add survey_feature(survey_id INT, feature_id TINYINT, PRIMARY KEY(survey_id, feature_id)). Insert the survey row first, grab its ID, then insert one row per selected checkbox. Also, switch away from the old mysql_* functions to mysqli or PDO with prepared statements.

// after inserting into survey_tbl with mysqli/PDO:
$surveyId = $mysqli->insert_id;
$featureIds = array_map('intval', $_POST['feature'] ?? []);
$ins = $mysqli->prepare('INSERT INTO survey_feature (survey_id, feature_id) VALUES (?, ?)');
foreach ($featureIds as $fid) {
    $ins->bind_param('ii', $surveyId, $fid);
    $ins->execute();
}

@diafol’s bitmask idea is also solid if you prefer a single integer column; it’s compact and fast. Trade-off: less human-readable and needs bit ops in queries. Choose the approach that fits how you’ll report and evolve the feature list.

Recommended Answers

All 4 Replies

<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 Member #120589

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.