i have manage to insert my checkbox array data into database. the problem is when i check multiple checkbox only the first data in array is inserted in database. i need when i check multiple checkbox, all the data i have check insert into database.can someone help me?

this is my php code:

    <?php

    include ('connect.php');

 $cat = implode(',', $_POST['check']);

 if(isset($_POST['submit']))
  {

    for($i=0; $i<count($cat);$i++)
{
$p_id =$_GET ['sitter'];
$price = $_POST ['price'];
$pickup_date =$_POST ['pickup_date'];
$dropoff_date =$_POST ['dropoff_date'];
$numdays = $_POST ['numdays'];
$total =$_POST ['test'];

$sql2 = "INSERT INTO cat_sitter(sitter_fk,cat_fk, price, date_in, date_out,total_day, total)VALUES ('$p_id','" . $cat[$i] . "','$price','$pickup_date','$dropoff_date','$numdays', '$total')" or die ("Error inserting data into table");

if ($conn->query($sql2) === TRUE) {
    echo "<script language='javascript'>alert('Succesfully Book.')
        window.location.replace(\"book_page.php\");
    </script>";
}else{

    echo "error: " . $sql2 . "<br>" . $conn->error;
 }
 }
          }
     ?>

this is my html code:

<?php
      include ('connect.php');

   $sql = "SELECT cat_id,name,gender,health_status,neutered,breed,color,age FROM cat WHERE owner_fk = '$id'";
  $result3 = mysqli_query($conn,$sql);
  $row6 = mysqli_fetch_array($result3);
  $cato = $row6['cat_id'];
  $result = $conn-> query($sql);

     if ($result-> num_rows > 0) {
     while ($row6 = $result-> fetch_assoc()) {

        //table code//

       echo "<td>" ."<input type='checkbox'  name= 'check[]' value='$cato'". "</td>";
       echo "</tr>";
     }
       echo "</table>";
    }
     else{
     echo "0 result";
    }
      $conn-> close();

     ?>

Dani AI

Generated

Two things are causing the symptom you describe. First, converting the checkbox array with implode() then doing count($cat) makes $cat a string, so the loop runs once and only the first element is processed. Second, the HTML generation sets the checkbox value from a variable that was fetched once before the loop ($cato) instead of using the current row’s cat_id inside the while, so every checkbox can carry the same value. already pointed out the broken table/<input> markup and recommended prepared statements — both are correct and should be applied.

A simple, safe backend pattern: treat $_POST['check'] as an array, validate/cast each item, and use a prepared statement inside a foreach. Adjust bind types to match your schema:

$cats = $_POST['check'] ?? [];
if (!empty($cats) && is_array($cats)) {
    $stmt = $conn->prepare(
        "INSERT INTO cat_sitter (sitter_fk, cat_fk, price, date_in, date_out, total_day, total) VALUES (?, ?, ?, ?, ?, ?, ?)"
    );
    // change the type string to match your columns (example: "iidsisd")
    $stmt->bind_param("iidsisd", $p_id, $cat_id, $price, $pickup_date, $dropoff_date, $numdays, $total);

    foreach ($cats as $raw) {
        $cat_id = (int) $raw; // ensure integer id
        $stmt->execute();
    }
    $stmt->close();
}

Troubleshooting checklist: ensure the checkbox value is <?= $row6['cat_id'] ?> inside the loop, the form uses method="post" and name="check[]", var_dump($_POST['check']) to inspect the array, and check $conn->error on failure. Sanitize and validate every input and use a transaction if the inserts must all succeed or all fail. This addresses both the client-side value bug and the server-side loop/SQL issues raised by and .

In your HTML code <table> not opened, table row <tr> not opened, invalid <input> tag without > - it should be something like

if($result->num_rows > 0){
    echo '<table>';
    while ($row6 = $result->fetch_assoc()){
        //table code//
        echo '<tr>';
        echo '<td><input type="checkbox"  name="check[]" value="'.$cato.'" /></td>';
        echo '</tr>';
    }
    echo '</table>';
}

PHP code $cat better use filtered variable

$cat = filter_input(
    INPUT_POST
    ,'check'
    ,FILTER_VALIDATE_INT
    ,FILTER_REQUIRE_ARRAY
);

other variables also filtered eg

$price = filter_input(INPUT_POST,'price',FILTER_SANITIZE_STRING);
$pickup_date = filter_input(INPUT_POST,'pickup_date',FILTER_SANITIZE_STRING);
...

Do not pass user input variables direct to SQL query - its a potential SQL injection risk! Use prepared statement instead - prepare, bind_param, execute - read manual MySQLi bind param or PDO bind param

commented: thanks for responding, really appreciate it. :) +0
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.