0

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.

Sayed Mohd Ali
  • 2,156
  • 3
  • 12
  • 28

2 Answers2

1

If you want to insert data in a single row

$subject = implode(',', $subjects);

this will return you single string with the separator ',' between every subject. Now you can Insert this whole in a single column.

And then retrieve it use explode function with ',': it will give you the same array $subjects;

Roman Skydan
  • 5,478
  • 4
  • 19
  • 39
0

I have simplified it to this level but the only problem is that i am getting no data in the data base. What is likely the problem/

if <?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'];
$others= $_POST['others'];

foreach ($others as $t){

    $sql1 = "INSERT INTO `trial_table` (`others`) VALUES ('".mysqli_real_escape_string($db,$t)."')";
mysqli_query($db,$sql1);
}

foreach ($subjects as $i){

    $sql = "INSERT INTO `trial_table` (`subjects`, marks) VALUES ('".mysqli_real_escape_string($db,$i)."' , '$marks')";
mysqli_query($db,$sql);
}


}
 ?>
<body>
    <form method="POST" action="test.php">

    <select id="multiselect" name="subject[]" multiple="multiple">
    <option value="Irrigation">Technology</option>
    <option value="Fertilizer">Science</option>   
     </select>
     <select id="multiselect" name="others[]" multiple="multiple">
    <option value="Ball">Sports</option>
    <option value="Netball">Gymics</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>