I am setting up a system that has many answers from a question. Therefore the user can click a button to dynamically add answers. I want the user to be able to tick a checkbox next to the answers which are correct and then insert this into the database (1 being correct).
Here is what I have:
HTML:
<div id="answers">
<label class="answer">
Answer:
<input type="text" name="ab_name[]" value=""/>
Correct?
<input type="checkbox" name="ab_correct[]" value="0">
</label>
</div>
PHP
$ab_name = $_POST['ab_name'];
$ab_correct = $_POST['ab_correct'];
$sql = "INSERT INTO answers_bank (`ab_name`, `ab_correct` ) VALUES (:ab_name, :ab_correct )";
$stmt = $db->prepare($sql);
foreach ($_POST['ab_name'] as $ab_name) {
$stmt->bindValue(':ab_name', $ab_name);
$stmt->bindValue(':ab_correct', $ab_correct);
$stmt->execute();
}
Like this:
The SQL inserts the ab_name
but the ab_correct
is ALWAYS set to 1
if it is ticked or unticked. Any guidance on this please?
Aucun commentaire:
Enregistrer un commentaire