-2

I want to enroll the student and insert the student id into Mysql database after I check and submit the checkbox value, but I already tried so many ways but still cannot...

This is the php code

<?php
if (isset($_POST['submitxd'])) {
    foreach ($_POST['enrol'] as $items) {
       $insert = $link->query("INSERT INTO student_course(studentID) values ('$items')");} 
}
?>

This is the html code

$result = $link->query("SELECT * FROM student WHERE programmeName = '$programme' AND intake = '$intake'");
while ($row = mysqli_fetch_array($result)) {

    echo "<tr>
                      <td>".$row['studentID']."</td>
                      <td>".$row['studentName']."</td>
                      <td>".$row['studentGender']."</td>
                      <td>".$row['studentContact']."</td>
                      <td>
                      <input type='checkbox' name='enrol[]' value='".$row['studentID']."'>
                      </td>                      
                      </tr>";
            }

Ramesh R
  • 7,009
  • 4
  • 25
  • 38
ica
  • 1
  • 1
  • Does this article help? HTML checkboxes have some unusual quirks. https://stackoverflow.com/questions/11424037/do-checkbox-inputs-only-post-data-if-theyre-checked – ChrisFNZ Aug 25 '19 at 06:36
  • thank you, but it does not work too.. – ica Aug 25 '19 at 06:45
  • Remove the square brackets from the name of the checkbox element within the HTML. Also, a checkbox is normally a 1/True if checked and a 0 (or sometimes doesn't even submit) if blank. Are you using jQuery as well? If so, type alert($('#enrol').prop( "checked" )); from the Browser console. – ChrisFNZ Aug 25 '19 at 06:56
  • 1
    use implodeand explode to insert or retrive checkbox value in the database – VIKAS KATARIYA Aug 25 '19 at 07:07
  • @ica so finally what problem do you geting?????? – Mohit Kumar Aug 25 '19 at 07:16
  • There are lots of errors in your approach. Firstly you need a form (with method POST and action of a receiving php file), secondly you need the receiving php file to use the $POST values (e.g. $_POST['studentId'] to then put a SQL query together and execute it. You have lots of errors in your approach at the moment. – ChrisFNZ Aug 25 '19 at 07:20

1 Answers1

-1

check whether your array contains values or not:

echo "<pre>";
print_r($_POST['enrol']);
echo "</pre>";

if not, you should write html code properly i.e. check form tag and its action path carefully and before submitting the form, remember to check out the checkbox

jalalkhan121
  • 67
  • 1
  • 6