Thursday, January 21, 2016

PHP ARRAYS: How To Save Multiple Arrays in Database

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">
<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 Code
<?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++;
?>

PHP ARRAYS: How To Save Multiple Arrays in Database

PHP ARRAYS: How To Save Multiple Arrays in Database Rating: 4.5 Diposkan Oleh: Emoblazz

0 comments:

Post a Comment