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.

7
  • what is your data in $_POST['farming']; and $_POST['subject'];? Commented Jan 8, 2019 at 11:17
  • Yeah,they are all data from different multiple select lists named farming[] and subject[] Commented Jan 8, 2019 at 11:19
  • please attach a print_r result with your question, Commented Jan 8, 2019 at 11:21
  • How i can i attach i am sorry if i may ask Commented Jan 8, 2019 at 11:24
  • do like this print_r($_POST['subject']); and then in your page you can see some data copy and past them here Commented Jan 8, 2019 at 11:26

2 Answers 2

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;

Sign up to request clarification or add additional context in comments.

Comments

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.