Sherlock Holmes
Sherlock Holmes

Reputation: 19

how to insert values from checkbox to MYSQL Database?

<table width="200" border="0">          
<tr>
  <td><p>
    <?php while($row1 = mysql_fetch_array($res)){ ?>
    <label>
      <input type="checkbox" name="module" value="<?php echo $row1['MID']?>" id="<?php echo $row1['MID']?>">
    <?php echo $row1['ModuleName']?></label>
    <br>
    <?php }?>
  </p></td>
</tr>
</table>

I use this code to outout the checkboxes,and the checkboxes appear allright.

I m not sure about how,the data can be saved to the MYSL table. the table i drew is in the form: student_module{studentID,ModuleID}

what is the code i should write to enter the data to that table. One student can enroll to many modules.

Upvotes: 0

Views: 6957

Answers (2)

Deepak Biswal
Deepak Biswal

Reputation: 4320

Using ajax functionality:

By clicking on checkbox collect the value of studentID and ModuleID and pass to the ajax method and then in your server end page, you can update db record.

Without ajax functionality

Post the form when user clicking on checkbox or put a save button below to submit the form on a PHP page and then you can collect all your values and update db accordingly.

Upvotes: 1

chirag ode
chirag ode

Reputation: 960

<input type="checkbox" name="chk1[]" value="" ><?php echo $row1['ModuleName']?>
if(isset($_POST['submit']))
{
$checkbox1 = $_POST['chk1'];
$selected_checkbox = "";
foreach ($checkbox1 as $checkbox1) 
{
$selected_checkbox .= $checkbox1 . ", ";
}
$selected_checkbox = substr($selected_checkbox, 0, -2);
 // now here in your insert query take mid='".$selected_checkbox."';
}

Upvotes: 0

Related Questions