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");

?>

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.