i have a prob with my checkbox

<form action="<?php echo $editFormAction; ?>" name="form2" id="form2" method="POST">
            <?php if ($totalRows_subjek > 0) { // Show if recordset not empty ?>
            <table width="69%"  border="0" cellspacing="0" cellpadding="0">
                <tr>
                    <td><div align="center">List Of Subject </div></td>
                </tr>
                <tr>
                    <td><div align="center"></div></td>
                </tr>
                <?php do { ?>
                <tr>
                      <td height="16"><div align="center">
                        <input name="subject_code" type="checkbox" value="<?php echo $row_subject['CODE']; ?>" checked="checked" />
                        <?php echo $row_subject['SUBJECT_NAME']; ?></div></td>
                </tr>
                <?php } while ($row_subject = mysql_fetch_assoc($subject)); ?>
                <tr>
                    <td><div align="center">
                      <input name="class" type="hidden" id="class" value="<?php echo 
                       $row_class['CLASS_NAME']; ?>" />
                      <input name="Submit" type="submit" id="Submit" value="Submit" />
                    </div></td>
                </tr>
                <tr>
                    <td height="16"><div align="center"></div></td>
                </tr>
                  </table>
                <input type="hidden" name="MM_insert" value="form2">
          </form>

i want insert all list when user checked
but this coding only insert the last data was checked

This is the example in the table when data is inserted:


Number | | |

(primary key) | Class | subject |

1 | A | List 1
2 | A | List 2

3 | A | List 3

i submit the value from checkbox as a 'text'

Please Help me....

    All checkboxes in your script have the same name, so PHP takes value of the last one. To prevent this, change your checkbox name to "subject_code[]" so it is treated as an array. After submitting the form, you can get values of selected checkboxes by foreach loop:

    if( is_array($_POST['subject_code']){
     foreach( $_POST['subject_code'] as $subject ){
      echo $subject . '<br />';    // change this to prepare insert query
     }
    }
    
      5 days later

      this time i try to check before insert the data
      but i failed yo do that

      this is my code:

      <?php
      session_start(); ?>
      <?php
      require_once('Connections/repi.php');
      
      // *** Redirect if username exists
      $MM_flag="Hantar";
      if (isset($_POST[$MM_flag])) {
        $MM_dupKeyRedirect="data_ada_kelas_subjek.php";
        $loginUsername = $_POST['kelas'];
      
      
        if(is_array($_POST['kod_subject'])){
        foreach( $_POST['kod_subject'] as $loginUsername2 ){
        $LoginRS__query = "SELECT KELAS,KOD_SUBJ FROM kelas_subjek WHERE KELAS='" . $loginUsername . "' AND KOD_SUBJ='" . $loginUsername2 . "' ";
         mysql_select_db($database_repi, $repi);
        $LoginRS=mysql_query($LoginRS__query, $repi) or die(mysql_error());
        $loginFoundUser = mysql_num_rows($LoginRS);
      }
      }
        //if there is a row in the database, the username was found - can not add the requested username
        if($loginFoundUser){
          $MM_qsChar = "?";
          //append the username to the redirect page
          if (substr_count($MM_dupKeyRedirect,"?") >=1) $MM_qsChar = "&";
          $MM_dupKeyRedirect = $MM_dupKeyRedirect . $MM_qsChar ."requsername=".$loginUsername;
          header ("Location: $MM_dupKeyRedirect");
          exit;
        }
      }
      
      
      
      
      
      include 'db_connect.php';
      // build the form action
      
      if($_POST['Hantar'] == "Hantar"){
      
      $kelas = $_POST['kelas'];
      if(is_array($_POST['kod_subject'])){
      foreach( $_POST['kod_subject'] as $kod ){
      $sql = mysql_query("INSERT INTO kelas_subjek (kelas, kod_subj)
      				VALUES ('$kelas', '$kod')");
      }
      }
      
      
      
      
      					if ($sql) {
      	include 'berjaya_daftar_kelas_subjek.php';
      	exit();
      } else {
      	echo mysql_error();
      }		  
      
      }
      
      ?>
      

      if all of the value have been in the database,it will go to the link that tells data have been inserted..
      but if user inserted 4 value which 3 value have in the database and 1 value not in database.it will insert all the 4 value in the database..
      please help me again..

        Write a Reply...