I have encountered saving a single array from a checkbox into the database. But in this example, I have here several input controls that are arrays. The code below contains 2 arrays, namely the letter and the choice. We will save it like this.
Output
choices |
HTML Code
<form method="post">PHP Code
<div class="col-md-1">
Choices
<input type="text" class="form-control" value="A" name="letter[]" readonly>
<input type="text" class="form-control" value="B" name="letter[]" readonly>
</div>
<div class="col-md-4">
Column B
<input type="text" class="form-control" name="choice[]">
<input type="text" class="form-control" name="choice[]">
</div>
</form>
<?php
$choice=$_POST['choice'];
$letter=$_POST['letter'];
$i=0;
foreach($choice as $c)
{
mysqli_query($con,"INSERT INTO answer(choice,letter) VALUES('$c','$letter[$i]')")or die(mysqli_error());
$i++;
?>
0 comments:
Post a Comment