I have created a checkbox using while loop and posting this to another page but I am not sure on how to add these ticked check box as 1 and non ticked one as 0 to the database. Can someone help me?

<form action="Test_Completed.php" method="post">

include '../Database/take_an_exam.php';
$intNumber = 1;
 while($info = mysql_fetch_array( $sql ))
 {
echo "$intNumber, {$info['Que_Question']} <br />\n";
echo "<input type=\"checkbox\" name=\"choice1[]\" value=\"{$info['Que_Choice1']}\" /> ";
echo "{$info['Que_Choice1']} <br />\n";
echo "<input type=\"checkbox\" name=\"choice2[]\" value=\"{$info['Que_Choice2']}\" /> ";
echo "{$info['Que_Choice2']} <br />\n";
echo "<input type=\"checkbox\" name=\"choice3[]\" value=\"{$info['Que_Choice3']}\" /> ";
echo "{$info['Que_Choice3']} <br />\n";
echo "<input type=\"checkbox\" name=\"choice4[]\" value=\"{$info['Que_Choice4']}\" /> ";
echo "{$info['Que_Choice4']} <br />\n";

$intNumber++; 
 }


 ?>
 <input type="submit" value="submit"/>
 </body>

 </html>

 </body>

when I post this, so far I have

<?PHP

$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("Examination", $con);

//Get & clean value from POST data

$choice1 = mysql_real_escape_string(trim($_POST['choice1']));
$choice2 = mysql_real_escape_string(trim($_POST['choice2']));
$choice3 = mysql_real_escape_string(trim($_POST['choice3']));
$choice4 = mysql_real_escape_string(trim($_POST['choice4'])); 
$user = mysql_real_escape_string(trim($_SESSION['username1'])); 


//Create and run INSERT query
$query = "INSERT INTO Answer (`Ans_Answer1`, `Ans_Answer2`, `Ans_Answer3`, `Ans_Answer4`, `Que_ID`, `Use_ID`) 
          VALUES ('{$choice1}', '{$choice2}', '{$choice3}', '{$choice4}', '{$query1}', '{$user}')";
$result = mysql_query($query) or die (mysql_error());

$_SESSION['Ans_ID'] = mysql_insert_id();


 header("location:check.php");

?>

Dani AI

Generated

Short answer: make each checkbox submit a predictable 0 or 1 tied to the question ID, then iterate the posted array and insert integers (not choice text) with prepared statements. The current problems are (a) unchecked boxes aren’t sent at all and (b) the HTML names don’t tie a specific question ID to each checkbox, so the server can’t reliably map answers to questions. is generating arrays of choices but then reading scalars, and is right that an unchecked box isn’t present in POST — that observation is useful but needs to be applied at scale.

A simple, reliable HTML pattern (one question/choice shown) is to emit a hidden field with value 0 and then the checkbox with value 1 using a name that includes the question id:

<input type="hidden" name="answers[123][1]" value="0" />
<input type="checkbox" name="answers[123][1]" value="1" />

On submit you will always get a 0 or 1 for answers[QUESTION_ID][CHOICE_INDEX].

Server-side workflow (summary):

  • Loop over $_POST['answers'] by question id and choice index.
  • Cast each value to (int) so it becomes 0 or 1.
  • Use prepared statements (PDO or mysqli) and a transaction to insert rows.
  • Store numeric IDs (question_id, choice_id) and the selected bit; avoid storing raw choice text.

Troubleshooting / best practices:

  • If a question is single-choice, use radio inputs instead of checkboxes.
  • Validate question IDs and choice indexes against your questions table before inserting to prevent tampering.
  • Prefer a normalized schema (one row per selected choice or a separate row per choice with a tinyint flag) rather than saving free text.
  • Replace deprecated mysql_* calls with PDO/mysqli and use parameterized queries for security.
  • If you need atomicity, wrap per-user answer inserts in a DB transaction so partial submissions don’t persist.

if I remember correctly, you can't get value for the check boxes if they're unchecked (Please correct me on this). You could try checking it if it's set.

if(isset($_POST['choice1'])){
   $choice1 = 1;
}else{
   $choice1 = 0; 
}
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.