I have a form with many multiple select lists. There are several multiple select lists(blocks) in the form. How can I submit the multiple select data to the database?
I have tried to create a foreach statement but the problem is it only submits one option in the database.
<?php
$db = mysqli_connect('localhost','root','','trial') or die($db);
if (isset($_POST['submit'])) {
$marks = mysqli_real_escape_string($db,$_POST['marks']);
$subjects= $_POST['subject'];
$farming= $_POST['farming'];
foreach ($subjects as $i) {
$subject = $i;
$sql = "INSERT INTO `trial_table` (`subjects`, `marks`) VALUES ('".mysqli_real_escape_string($db,$subject)."', '$marks')";
mysqli_query($db,$sql);
}
$sql1 = "INSERT INTO `trial_table` (`marks`) VALUES ('$marks')";
mysqli_query($db,$sql1);
}
?>
<body>
<form method="POST" action="test.php">
<select id="multiselect" name="farming[]" multiple="multiple" required>
<option value="Irrigation">Irrigation</option>
<option value="Fertilizer">Fertilizer</option>
<option value="Pesticide">Pesticide</option>
</select>
<select id="multiselect" name="subject[]" multiple="multiple" required>
<option value="Irrigation">Technology</option>
<option value="Fertilizer">Science</option>
</select>
<div class="input-group">
<label>Marks</label>
<input type="number" name="marks">
</div>
<button type="submit" name="submit" class="btn">SUBMIT</button>
</form>
</body>
</html>`enter code here`
I expect to the Form to submit the selected data not just one to the database.