Binty 0 Newbie Poster

**how to check data by multivalue of array checkbox in multi row one column.
**
Hi, im need to checked data from database from checkbox multivalue of array, then insert into tabel in multirow one column.
I try to insert same value (month) in student payment but the data still insert to the tabel, and it should show alerts.
I confusing how to check data with array and how show the alert, in this case?
pls help...
I'm new here..
and this is my code.

<!--form -->

<tr>
 <td>ID Student</td>
 <td>:</td> 
 <td><input name="id_student" type="text" id="nis" value="<?= $row['id_student']?>" readonly="readonly"/></td>
</tr>
<tr>
 <td>Month</td>
 <td>:</td>
 <td>
  <label class="checkbox inline"><input type="checkbox" name="month[]" value="1"> Jan
  </label>
  <label class="checkbox inline"><input type="checkbox" name="month[]" value="2"> Feb</label>
  <label class="checkbox inline"><input type="checkbox" name="month[]" value="3"> Mar</label>
  <label class="checkbox inline"><input type="checkbox" name="month[]" value="4"> Apr</label>
  <label class="checkbox inline"><input type="checkbox" name="month[]" value="5"> Mei</label>
  <label class="checkbox inline"><input type="checkbox" name="month[]" value="6"> Jun</label>
  <label class="checkbox inline"><input type="checkbox" name="month[]" value="7"> Jul</label>
  <label class="checkbox inline"><input type="checkbox" name="month[]" value="8"> Agt</label>
  <label class="checkbox inline"><input type="checkbox" name="month[]" value="9"> Sep</label>
  <label class="checkbox inline"><input type="checkbox" name="month[]" value="10"> Okt</label>
  <label class="checkbox inline"><input type="checkbox" name="month[]" value="11">Nov</label>
  <label class="checkbox inline"><input type="checkbox" name="month[]" value="12">Des</label>
</td>

/form action/

    <?php

    $transaction_id = $_POST['transaction_id'];
    $id_student     = $_POST['id_student'];
    $spp            = $_POST['spp'];
    $praktek        = $_POST['praktek'];
    $tabungan       = $_POST['tabungan'];
    $osis           = $_POST['osis'];
    $month          = $_POST['month'];

    $sum = count ($month);
    echo $sum;

    if (isset($_POST['month'])){
        $checkedPay =mysql_num_rows(mysql_query
            ("SELECT m.id_month 
            FROM month_pay m
            INNER JOIN transaction t
            INNER JOIN student s 
            ON t.id_student = s.id_student
            AND m.transaction_id = t.transaction_id where m.id_month='$month' AND t.id_student='$id_student'"
        ));     
        if($checkedPay>0){ 
             echo "have been paid.";
             echo "<script type=\"text/javascript\">alert('failed!');"."history.go(-1)</script>";
        }
        else{
            $query1=mysql_query ("insert into transaction(transaction_id , id_student, nominal_spp, nominal_praktek, nominal_tabungan, nominal_osis, date_pay ) 
            values('','$id_student','$spp', '$praktek', '$tabungan', '$osis', SYSDATE() )");            
            if($sum >=1){
                foreach ($month as $a ){
                    mysql_query("insert into month_pay (transaksi_id, id_month) values(LAST_INSERT_ID(), '$a')");
                }
            } 
        }
    }
    ?>

Dani AI

Generated

Quick diagnosis for : the checkbox month[] arrives as an array in PHP. Using it directly inside a SQL string (for example m.id_month = '$month') will not match the values — PHP will cast the array to the string "Array" and the query will fail to find the selected months. Also, the old mysql_* functions are deprecated and make safe parameter binding harder. The reliable approach is: (1) validate and normalize the posted month values, (2) run one prepared query that checks which of the selected months are already recorded for that student (use IN (...)), (3) compute which months remain, and (4) insert only the new months inside a DB transaction so you do not get partial state.

Compact PDO workflow example (conceptual; adapt column/table names to your schema):

$months = $_POST['month'] ?? [];
$idStudent = (int) $_POST['id_student'];

if (!$months) exit('No months selected');

$placeholders = implode(',', array_fill(0, count($months), '?'));
$sql = "SELECT m.id_month
        FROM month_pay m
        JOIN transaction t ON m.transaction_id = t.transaction_id
        WHERE t.id_student = ? AND m.id_month IN ($placeholders)";
$stmt = $pdo->prepare($sql);
$stmt->execute(array_merge([$idStudent], $months));
$already = $stmt->fetchAll(PDO::FETCH_COLUMN);

$toInsert = array_values(array_diff($months, $already));
if (empty($toInsert)) { /* show alert: months already paid */ }

// begin transaction, insert transaction row, then insert only $toInsert months, commit

Extra notes: enforce uniqueness at the DB level (unique index on student+month or transaction_id+month) so duplicates are blocked even under race conditions; catch duplicate-key errors and present a friendly message. Migrate to PDO or mysqli prepared statements for safety and stability (see PHP manual on prepared statements: PDO prepared statements). Also double-check column name consistency between your transaction and month_pay tables to avoid silent mismatches.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.