如何将复选框数组的值插入数据库,同时还从数据库中检索了 cheekboxs 值?

How to insert value of the checkbox array into the database while the cheekboxs value also have been retrieved from the database?

我正在从 database.I 中检索查询结果,已将记录的 ID 存储到复选框数组中。 我能够检索数据并将其存储到复选框数组 successfully.now 我想将复选框的值插入不同的 table.In 下面的代码 我使用了 for 循环来插入数据和 implode 函数来检索数组元素,但是当我 运行 这个脚本时,它没有显示任何错误,但是数据没有插入到数据库中。 谁能告诉我我做错了什么?我该如何解决?

<?php

    $conn = mysqli_connect('localhost:3306', 'root', '', 'mydatabase');
    if($conn -> connect_errno){
        die('coudn\'t connect to the database' . mysqli_connect_error());
    }
    else{
        $query = "select * from simple";
        $result = $conn -> query($query);
            echo '<table border="1px"><tr><td>Select</td><td>ID</td><td>Name</td><td>Surname</td></tr>';
            while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
                echo '<tr>';
                echo "<td><input type=\"checkbox\" name=\"checkbox[]\" id=\"checkbox[]\"  value=\"".$row['id']."\" /></td>";                   
                echo "<td>{$row['id']}</td> "."<td>{$row['Name']}</td>"."<td>{$row['Surname']}</td>";
                echo '</tr>';
            }
            echo '</table>';
            echo "Fetched data successfully\n";
            $limit = count('$checkbox');
            echo $limit;
            for($i=0;!$limit > 0;$i++,$limit--){
                $res = implode(",", '$checkbox');
                $q1 = 'insert into joint_data values("'.$i.'","'.$res.'")';
                if($conn -> query($query) == TRUE){
                        echo "Data Entered successfully\n";
                    }
                echo $i,$res;
            }
        }
        $result->free();
    mysqli_close($conn);
  ?>

$conn = mysqli_connect('localhost:3306', 'root', '', 'mydatabase');
if($conn -> connect_errno){
    die('coudn\'t connect to the database' . mysqli_connect_error());
}
else{
    if(!isset($_GET['checkbox'])){
    $query = "select * from simple";
    $result = $conn -> query($query);
        echo '<form action="" method="get"><table border="1px"><tr><td>Select</td><td>ID</td><td>Name</td><td>Surname</td></tr>';
        while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
            echo '<tr>';
            echo "<td><input type=\"checkbox\" name=\"checkbox[]\"  value=\"".$row['id']."\" /></td>";                   
            echo "<td>{$row['id']}</td> "."<td>{$row['Name']}</td>"."<td>{$row['Surname']}</td>";
            echo '</tr>';
        }
        echo '<td><input type="submit" value="submit" name="submit" /></td></table></form>';
        echo "Fetched data successfully\n";
}else{
    $data = $_GET['checkbox'];
        $query = "select * from simple";
        $result = $conn -> query($query);
    $i=1;
        while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
            if (in_array($row['id'], $data)) {
        $q1 = 'insert into joint_data values("'.$i++.'","'.$row['id'].'")';
        if($conn -> query($query) == TRUE){
                    echo "Data Entered successfully\n";
                }
                echo $i,$row['id'];
    }
        }
}

$result->free();
mysqli_close($conn);

    }

这将是添加表单后的代码

<?php
$conn = mysqli_connect('localhost:3306', 'root', '', 'mydatabase');
if($conn -> connect_errno){
    die('coudn\'t connect to the database' . mysqli_connect_error());
}
else
{
    $query = "select * from simple";
    $result = $conn -> query($query);
?>
        <html>
        <body>
        <form name="frmedit" method="post" action="">       
        <table border="1px">
        <tr>
            <td>Select</td>
            <td>ID</td>
            <td>Name</td>
            <td>Surname</td>
        </tr>
        <?php   
        while ($row = $result->fetch_array(MYSQLI_ASSOC)) { ?>
            <tr>
                <td>
                    <input type="checkbox" name="checkbox[]" id="checkbox[]"  value="<?php echo $row['id'];?>"/>
                </td>                   
                <td><?php echo $row['id'];?></td>
                <td><?php echo $row['Name'];?></td>
                <td><?php echo $row['Surname'];?></td>
            </tr>
       <?php } ?>
        </form>
        </body>
        </table>
       <?php echo "Fetched data successfully\n";?>
        </html>
       <?php 
        $limit = count($checkbox);
        echo $limit;
        for($i=0;!$limit > 0;$i++,$limit--){
            $res = implode(",", $checkbox);
            $q1 = 'insert into joint_data values("'.$i.'","'.$res.'")';
            if($conn -> query($query) == TRUE)
            {
                echo "Data Entered successfully\n";
            }
            echo $i,$res;
        }
    }
$result->free();
mysqli_close($conn);
?>